View Full Version : Validating a checkbox and then changing a value

07-30-2008, 07:39 AM

I've been trying to write a script that checks if a checkbox is selected and if it is, it sets the value of a quantity field to 1. I am using php and was wanting to set the action prior to the form being sent.

If a user selects and item, then I want the quantity to be set to 1 with an onclick event. I've tried several things and keep getting errors. I finally wrote something that doesn't give an error (using firebug in firefox) but it still doesn't set the value to 1.

Here is the form code:

// Fetch and print all the records.
$ix = 0; // form field index counter
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
echo '<tr><td align="left">' . $row['category'] . '</td>
<td align="left"><input type="checkbox" name="od['.$ix.'][selection]" value="' . $row['dn'] .'" onclick="setvalue(checked)"></td>
<td align="left"><input type="text" name="od['.$ix.'][qty]" id="od_' .
$row['d1'] . '_' . $row['price'] . '" size="2" value="" onchange="CalculateTotal(this.form)"></td>
<td align="left">' . $row['dn'] . '</td>
<td align="left">' . $row['dd'] . '</td>
<td align ="right">' . $row['price'] . '</td></tr>';
$ix++; // increment index

NOTE: I have another JS that calculates the total order based on the qty selected -- this is the field that I want changed to 1 if the user clicks the checkbox for the item.

I've been reading and researching and testing for the past 6 hours and am stumped. I've got this so far to not give an error but don't have a clue where to go from here.

<script language="javascript" type="text/javascript">
function setvalue(checked) {

if document.menu.od.selection.checked=true {
document.menu.od.quantity.value == 1;

Maybe I'm braindead but I think I just don't have the experience with JavaScript and PHP meshing together to get the variables set correctly in order to change the values.

I am also wanting to have the checkbox show as selected if a user inputs a value into the qty field. This is just so it stays user friendly as many may forget one or the other and both are needed to pass values to the db as is reflected in this portion of the code:

// Get the order, print it and send to the database
foreach($_POST['od'] as $order)
$qty = $order['qty']; // this is the quantity field
$sel = $order['selection']; // this is the checkbox field and sends the value of the dish_name to the db

if ($qty > 0) { // Don't send zero values
echo ' ' . $qty . ' ' . $sel . ' <br />';
$query = "INSERT INTO order_details (od_id, order_id, dish_name, od_qty, order_date) VALUES ('', '$oid', '$sel', '$qty', NOW())";
$result = @mysql_query ($query); // Run the query

Any help would be appreciated.

07-30-2008, 07:48 AM
First, you should stop using language attribute as it's deprecated (remove highlighted in red):

<script language="javascript" type="text/javascript">
function setvalue(checked) {

if document.menu.od.selection.checked=true {
document.menu.od.quantity.value == 1;

Also, you must have been confused. = is used to assign values and == is used to compare the equality. Replace the highlighted above with:

if (document.menu.od.selection.checked==true) {
document.menu.od.quantity.value = 1;

You were sending a parameter true on setvalue function, but you haven't used it. You better remove it.

Hope that helps.

07-30-2008, 09:23 PM
Thanks will try that and let you know. In the meantime I just went with a php fix by removing the checkbox completely and passing the data values in an array.