Gin.A Gin.A - 2 months ago 8
HTML Question

Dropdown data not being sent to $_POST php

I'm new to coding, and have encountered some difficulty with the drop-down list. Would appreciate any help given!

I have this:

<html>
<select name="Subject">
<option value="One">One</option>
<option value="Two">Two</option>
</select>
</html>

<?php
if (isset($_POST['submit'])) {
echo $_POST['Subject'];
}
echo '
<form method="post"><input type="submit" name="submit" value="Submit Option!"></form>';
?>


This returns me with an unidentified index error for 'Subject' whenever I hit the Submit Option button.
I did a
print_r($_POST)
and realized that my selected options for the drop-down list "Subject" did not pass through. (i.e. The
$_POST
array that was printed did not show anything selected options from the drop-down list)

Answer

Put <form> code above the <select> like below:-

<html>
    <form method="post">

    <select name="Subject">
    <option value="One">One</option>
    <option value="Two">Two</option>
    </select>

    <input type="submit" name="submit" value="Submit Option!"></form>
</html>
<?php
if (isset($_POST['submit'])){
echo $_POST['Subject']; 
}
?>