Page 1 of 1
Getting values from option tags in an HTML form
Posted: Thu Jun 19, 2008 4:54 pm
by SpatulaOfDoom
Hey guys, I've done quite a bit of googling for an answer to this and as yet haven't found anything.
I've generated a html form including a drop down menu which looks a little like this
Code: Select all
<select name='choice'>
<option value='1'>an option</option>
<option value='2'>a second option</option>
</select>
I want to be able to get the value from the option as opposed to the text (e.g. 1 instead of "an option"), but when I use $_POST['choice'] it gives me "an option".
The reason I want this is that I want to present the user with a nice choice, but just insert '1' into my database.
Can anyone help me out here?
Many thanks
Re: Getting values from option tags in an HTML form
Posted: Thu Jun 19, 2008 5:16 pm
by WebbieDave
Well, that's odd. The browser should be sending choice=1. We'll need to see your code for further investigation.
Re: Getting values from option tags in an HTML form
Posted: Thu Jun 19, 2008 5:32 pm
by SpatulaOfDoom
Well here's the full code (it's not particularly nice with bits of php and html mixing up)
Code: Select all
<?php
include('../../includes/config.php')
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>New Menu Item</title>
</head>
<body>
<?php
## checking to see if form has already been posted
if(isset($_POST['submit'])){
## check to see if everything has been posted
$check = !empty($_POST['name']);
if($check){
## add item to db
echo $_POST['parent']."<br>";
$query = sprintf("INSERT INTO menu (name, type, parentId) VALUES ('%s', '%s', '%d')",
mysql_real_escape_string($_POST['name']),
mysql_real_escape_string($_POST['type']),
$_POST['parent']);
$result = mysql_query($query) or die ($query);
echo "Menu Item ".$_POST['name']." added";
}else{
## not everything is filled in
echo "not all the fields were filled in, try again <a href 'new.php?act=new>go back</a>";
}
} else {
$query = "SELECT menuId, name FROM menu WHERE type LIKE 'menu' ORDER BY menuId ASC";
$result = mysql_query($query);
?>
<form method='POST' action='new.php?' act='post'>
<h1>New Menu Item</h1>
Item name:<br/>
<input type='text' name='name' size='25'><br/>
Type:<br/>
Page<input type='radio' name='type' value='page' checked='checked'>
Menu<input type='radio' name='type' value='menu'><br/>
Parent menu:
<select name='parent'>
<?
while ($row = mysql_fetch_array($result)){
echo "<option vaulue='".$row['menuId']."'>".$row['name']."</option>";
}
?>
</select><br/>
<input type='submit' name='submit' id='submit' value='Add Menu Item'>
</form>
<?php
}
?>
</body>
</html>
Re: Getting values from option tags in an HTML form
Posted: Thu Jun 19, 2008 6:24 pm
by WebbieDave
In your code, the attribute value has been misspelled as vaulue.
Re: Getting values from option tags in an HTML form
Posted: Thu Jun 19, 2008 6:30 pm
by SpatulaOfDoom
WOW, I feel so so so stupid. Thanks for the help, I'm going to hang my head in shame now.
Re: Getting values from option tags in an HTML form
Posted: Thu Jun 19, 2008 6:43 pm
by WebbieDave
We all make those kinds of mistakes from time to time
