-1

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?

user1214840
  • 189
  • 1
  • 6
  • 14

1 Answers1

0

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.

dtech
  • 13,741
  • 11
  • 48
  • 73
  • Added it, through post is usually the right choice for forms. – dtech Mar 27 '13 at 19:33
  • That is simply not the case, and your 255 character limit is nonsense, too. – Grant Thomas Mar 27 '13 at 19:37
  • Option 1 is correct markup. – crafter Mar 27 '13 at 19:56
  • @GrantThomas RFC 2616 specifies 255 as the advisable limit (http://www.faqs.org/rfcs/rfc2616.html), altough it seems 2000 is the current safe choice (http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers). A GET request should only retrieve data, not alter it. If the server stores/alters data through the firm (which is often the case) POST should be used. – dtech Mar 27 '13 at 22:33
  • 1
    @crafter is perfectly valid HTML (http://www.w3.org/TR/html5/forms.html#attr-option-value) – dtech Mar 27 '13 at 22:34