简体   繁体   中英

Check if the form is submitted or not with PHP

I use this select box to select an option from it and display contents according to selection. I am not using a submit button submit this form, thats why I have added onsubmit/onchange feature to this drop down box.

This is my drop down box :

<select class="select" name="type" onchange="document.forms[0].submit();">
    <option value="1">Tutor</option>
    <option value="2">Institute</option>
    <option value="3">Student</option>
</select>

can anybody tell me how I can check this select box have selected an option or not with Javascript? Of course I need to make sure to check if the form was submitted or not with PHP.

使用isset

if (isset($_POST["type"])) ...

Use jquery like this:

$("select[name='type']").change(function(){
    if($(this).val() != ""){
        $(this).parent().submit();   
    }
});

HTML:

<form>
<select class="select" name="type">
    <option value="">Select one</option>
    <option value="1">Tutor</option>
    <option value="2">Institute</option>
    <option value="3">Student</option>
</select>
</form>

Why not make an 'echo' from the PHP code?

echo $_POST['type'];

From your PHP code. I'm not sure if this is correct, but I believe you should get some response: '1', '2' or '3', depending on the value submitted. I don't remember very well the Radio, forgive me.

You can check for the POST being submitted from the select's name and then check it's an integer like so:

if(isset($_POST['type'] && is_int($_POST['type']) )
  {
   // It's been submitted and you can do your processing 
  }

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM