I'm trying to set up an html form that allows the user to select a field from a drop-down menu, then specify the information for that field. For example, if they select "Name" from the menu, then enter a name, it posts a specific response.
Here's how the html looks:
<form action="hello.php" method="post">
<select>
<option disabled="yes" selected="yes">Select...</option>
<option value="name">Name</option>
<option value="gender">Gender</option>
<option value="birthday>Birthday</option>
<input name="name" type="text" />
<input type="submit" value="Submit">
</form>
What I'm trying to do is have the php code change responses based on what's submitted. Here's the php code:
$name = $_POST['name'];
$gender = $_POST['gender'];
$bday = $_POST['birthday'];
With the responses:
echo "Hello, $name!";
echo "Your gender is $gender!";
echo "Happy birthday on $birthday!";
depending on what option they select. (I haven't finished the if/elseif/else statements yet because I'm not sure if they're influenced by how the information is recieved.)
Is it possible to do this? I know that my code will only ever return $name in this example, because
<input name="name"...>
But I don't know if it's possible to have the text field's information identified by the drop-down menu. I imagine it's probably possible with Javascript, but I've never used Javascript and wouldn't know how to do this. Thanks!
EDIT: I found an answer. I ended up setting
<input type="text" name="submit" />
in my html code, and
$submit = $_POST['submit'];
in my php code. Now if I do something such as
echo $submit;
it returns whatever I entered in the html page. Thanks again!
<form action="hello.php" method="post">
<select name="myselectbox">
<option disabled="yes" selected="yes">Select...</option>
<option value="name">Name</option>
<option value="gender">Gender</option>
<option value="birthday>Birthday</option>
<input name="name" type="text" />
<input type="submit" value="Submit" />
</form>
Then in hello.php do this at the top of the file.
var_dump($_POST);
that should help you understand the relationship between a html form and the PHP Postback Form Handler (in your case hello.php)