Jump to content


Photo

getting the value of menu before passing to a database


  • Please log in to reply
2 replies to this topic

#1 musashine

musashine
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 07 June 2006 - 05:52 PM

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.
[!--fonto:Comic Sans Ms--][span style="font-family:Comic Sans Ms"][!--/fonto--][!--sizeo:2--][span style="font-size:10pt;line-height:100%"][!--/sizeo--]Life's full of crap, either learn to live with it or find a way to get around it!![!--sizec--][/span][!--/sizec--][!--fontc--][/span][!--/fontc--]

#2 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 07 June 2006 - 06:45 PM

So you want to get an option someone selects, put it into a variable and then insert that into the database?

<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>

Now the script side

<?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
 }
?>

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.

#3 musashine

musashine
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 07 June 2006 - 07:43 PM

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\" /]
[!--fonto:Comic Sans Ms--][span style="font-family:Comic Sans Ms"][!--/fonto--][!--sizeo:2--][span style="font-size:10pt;line-height:100%"][!--/sizeo--]Life's full of crap, either learn to live with it or find a way to get around it!![!--sizec--][/span][!--/sizec--][!--fontc--][/span][!--/fontc--]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users