View Full Version : Passing values from drop down menu and checkbox
devil_vin
10-27-2007, 08:49 AM
Hey,guys! I have a drop down menu and checkbox,how could I passed the selected value to database by POST in INSERT statement?Thanks...
<select name="category" title="category">
<option selected>U</option>
<option>18SG</option>
<option>18SX</option>
<option>18PA</option>
<option>18PL</option>
</select>
<input type="checkbox" name="CHK1" value="checkbox">12:00pm
<input type="checkbox" name="CHK2" value="checkbox">1:00pm
<input type="checkbox" name="CHK3" value="checkbox">2:00pm
boogyman
10-27-2007, 02:00 PM
with the dropdown you just need to assign a value to the option.
<select name="category" title="category">
<option value="U" selected="selected">U</option>
<option value="18SG">18SG</option>
<option value="18SX">18SX</option>
<option value="18PA">18PA</option>
<option value="18PL">18PL</option>
</select>
the value is the field that you need to change, not the name.
<input type="checkbox" name="time[]" value="12:00">12:00pm
<input type="checkbox" name="time[]" value="13:00">1:00pm
<input type="checkbox" name="time[]" value="14:00">2:00pm
also its better if you give the checkbox a meaningful name so that you / anyone else that may work on the script in the future will know what that is supposed to be.
that would get it to the server-side, then you could edit it how you needed to on the back-end
<?php
$category = $_POST'category'];
$time = $_POST['time']; // this contains an array of each of the checkboxes selected
_connect_to_database_
$insert = "INSERT INTO tbl(fields) VALUES(values)";
if( !mysql_query($insert) ) {
echo "error inserting data";
}
else {
doSomething();
}
?>
devil_vin
10-27-2007, 02:43 PM
Thank for replying, is it neccessary for drop down menu to have selected="selected" ?
How about if INSERT statement mix with $_POST and $variable?
I having error on my INSERT statement.
$category = $_POST['category'];
$time = $_POST['time'];
$insert = mysql_query("INSERT INTO movie(name,director,casting,synopsis,screeningTime,classification) VALUES ('" . $_POST['title'] . "','" . $_POST['director'] . "',
'" .$_POST['casting'] . "','" . $_POST['synopsis'] ."' ,'" . $time . "',
'" . $category ."')") or die(mysql_error());
boogyman
10-27-2007, 08:04 PM
the ="selected" is proper coding technique, but i dont know of any browser that will not allow it.
same with checked="checked" on radio/checkbox
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.