1

I am creating a form which when submitted sends the output to an INSERT statement. I am learning how to do this using:

<form action="somepage.php" method="post">
Example: <input type="text" name="something" />
<input type="submit" />
</form>

I am fairly comfortable doing this and then writing an INSERT statement containing $_POST.

I also have a field that I would like to present in a drop down which is populated with the results of a mysql_query, as below:

$sql = "SELECT name FROM customer";
$result4 = mysql_query($sql);

echo "<select name='name'>";
while ($row = mysql_fetch_array($result4)) {
echo "<option value='" . $row['name'] . "'>" . $row['name'] . "</option>";
}
echo "</select>";

However I am not sure how to use the value that the user selects. I.e. I would like to use the value that they select in my INSERT statement along with the other form fields.

How do I do this?

1

2 Answers 2

1

You specify in the <select> tag the name, like:

<select name="car">
    <option>Audi</option>
    <option>BMW</option>
</select>

And get the submitted value with $_POST['car']

In your case, after submitting you can use $_POST['name']

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for that. I'm still a little confused though (beginner...). Where do I put the drop down code in relation to the rest of the form/submit button? How does the code send the value to somepage.php without it being specified as with action="somepage.php"?
Would this work?: <form action="somepage.php" method="post"> Example: <input type="text" name="something" /> <?php echo "<select name='name'>"; while ($row = mysql_fetch_array($result4)) { echo "<option value='" . $row['name'] . "'>" . $row['name'] . "</option>"; } echo "</select>"; ?> <input type="submit" /> </form>
0

Use $_POST['name'] to get the submitted value.

1 Comment

The problem is I don't know how to put the bits of the form together. I.e. where is the 'Submit' button? Where is the action="..."?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.