Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Validating a checkbox and then changing a value

    Hello,

    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:
    PHP Code:
    // Fetch and print all the records.
        
    $ix 0// form field index counter
        
    while($row mysql_fetch_array($resultMYSQL_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.

    Code:
    <script language="javascript" type="text/javascript">
    function setvalue(checked) {
    	
    	if document.menu.od.selection.checked=true {
    	document.menu.od.quantity.value == 1;
    	}
    }
    </script>
    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:

    PHP 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.
    Last edited by SuzyQueue; 07-30-2008 at 07:43 AM.

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    First, you should stop using language attribute as it's deprecated (remove highlighted in red):
    Code:
    <script language="javascript" type="text/javascript">
    function setvalue(checked) {
    	
    	if document.menu.od.selection.checked=true {
    	document.menu.od.quantity.value == 1;
    	}}
    </script>
    Also, you must have been confused. = is used to assign values and == is used to compare the equality. Replace the highlighted above with:
    Code:
    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.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #3
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •