I need to make what the user selects from a HTML select drop down menu, into a php variable that I can then use in an sql INSERT statement.
How could I do this?
I need to make what the user selects from a HTML select drop down menu, into a php variable that I can then use in an sql INSERT statement.
How could I do this?
Use a form. You can then access the variable through the $_POST
superglobal.
<form action="targetpage.php" method="post">
<select name="myselect">
<option>Option 1</option>
<option>Option 2</option>
</select>
<input type="submit">
</form>
targetpage.php:
<?php
echo $_POST['myselect']; // Will print either "Option 1" or "Option 2"
?>
edit
You can either use "post" or "get" for form data. get alters the URL, which you want to keep to 2000 characters or less. Post sends the data separate from the URL. If you use get you can retrieve the data through $_GET
, but for forms post is usually the right option.