Reputation: 65
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)
Upvotes: 3
Views: 1700
Reputation: 72289
To submit your selected value to PHP you need to put <select>
inside <form>
code 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['Subject'])){
echo $_POST['Subject'];
}
?>
Upvotes: 3
Reputation: 10084
One of the first things to know about HTML forms is that, when a form is submitted, the information contained within it gets submitted. To submit a value for Subject, that field needs to be contained within the <form>
element.
<html>
<?php
if (isset($_POST['submit'])) {
echo $_POST['Subject'];
}
?>
<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>
Upvotes: 3