Jump to content

getting the value of menu before passing to a database


musashine

Recommended Posts

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.