Pass value by POST with select option PHP

Asked

Viewed 10,772 times

0

I want to pass the name of the options by POST to another page. I used this but does not recognize the variable.

echo "<form method=POST action=_search.php>";
echo "<select name=selectoption>";
    echo "<option value=nom>nome</option>";
    echo "<option value=prenom>ultimo</option>";
echo "</select>";
echo "<input type=submit name=btnsearch value=search >";
echo "</form>";

And on the other page to receive the value,

$var = $_POST['selectoption'];
echo $var;  
  • The number of the selected option or all options? What error are you showing? I did a test with this code of yours and here it worked without errors.

  • I added the name in each option, and give me this error Undefined index: selectoption

  • 1

    already solved, was the name of the options. Thank you

1 answer

3

Apparently, your problem is that you’re confusing value with the description of its <option> because I tested these two codes here and it’s working:

pregnant

and

nom

In this case, you have the option 'name' and 'last' only with values of 'nom' and 'prenom' which are the values returned by $_POST['selectoption'];

So if you want to receive 'name' and 'last' instead of 'nom' and 'prenom' you should use the following html in your <option>:

echo "<select name=selectoption>";
echo "<option value=nome>nome</option>";
echo "<option value=ultimo>ultimo</option>";
echo "</select>";

by submitting the form you will have as a result:

name

or

last

Observing:

I used your codes to perform the tests, so all the above statements are correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.