Jump to content

Archived

This topic is now archived and is closed to further replies.

musashine

getting the value of menu before passing to a database

Recommended Posts

Hi im new at php and mysql and i was wondering if i can get the selected option of a menu and making it a variable before passing it to a database?

Thanks, Any help would be much appreciated.

Share this post


Link to post
Share on other sites
So you want to get an option someone selects, put it into a variable and then insert that into the database?

[code]
<form action="form.php" method="post">
<select name="dropdown">
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
  <option value="3">Option 3</option>
</select>
<input type="submit" value="submit" name="submit" />
</form>
[/code]

Now the script side

[code]
<?php
// Check to see if user clicked the submit button
if(isset($_POST['submit'])) {
  // Get the option the user selected
  $option = $_POST['dropdown'];
  
  // Insert the option into the database
  // You will also need a database connection setup before this step....
  $strqry = "INSERT INTO myTable (id, option) VALUES(null, '{$option}')";
  $query = mysql_query($strqry) or die("MYSQL Error: <br />{$strqry} <br />". mysql_error());
} else {
    // Display the form to the user
    // Insert above HTML here to display the form
}
?>
[/code]

So $option will be 1, 2 or 3 depending on what the user selected in the drop down box when they click on the submit button.

Though I am assuming you mean 'menu' as in a drop down menu on a HTML form of course.

Share this post


Link to post
Share on other sites
Thank you very much! SharkBait, that code was just what i needed [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.