Hello again,

I'm a bit stumped on some coding. What I am wanting to do is to validate that both a checkbox is checked and a quantity is entered. Here's my form code:

$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'] .'"></td>
        <td align="left"><input type="text" name="od['.$ix.'][qty]" id="od_' .
        $row['d1'] . '_' . $row['price'] . '" size="2" 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
	}

I have 2 options that I want to do and must compare the 2 fields:

1) If a qty >0 is entered, then set the checkbox to checked or
2) If the checkbox is checked then set the qty to 1 as a default but still can enter any qty.

I've tried several things but nothing is working and I'm fairly sure it is because I'm not writing the variable for these correctly or not in the right place. The array values are set in a foreach statement in the process_form script:

// Get the order, print it and send to the database
		foreach($_POST['od'] as $order)
{
    $qty = $order['qty'];
    $sel = $order['selection'];

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())";

Any help would be appreciated. BTW you guys helped me on this code before and I got the rest working. Thanks for that!

    1) If a qty is entered, then set the checkbox to checked or
    2) If the checkbox is checked then set the qty to 1

    Do you mean you want to do this when the enduser checks the checkbox or the enduser sets the quanitity to 1? Or do you mean, you want your script (from the server before the html is sent to browser) to render the checkbox as checked on a quantity of one or greater?

      When the enduser checks the checkbox or the enduser sets the quantity to 1 or more

      I want it to be user friendly and we know most may check and not enter a qty or vice versa lol.

      1) If a qty >0 is entered, then set the checkbox to checked or
      2) If the checkbox is checked then set the qty to 1 as a default but still can enter any qty.

        So, what information does the checkbox provide that can't be determined from the quantity? What it sounds like is that "the checkbox is checked" should be exactly equivalent to "the quantity is one or more"; any test that involves looking at the first condition could look at the second condition instead and get the same result. With nothing looking at the checkbox any more, there wouldn't be any point having it at all.

        If you want to make sure that users do both, there's no point having the computer do it for them; if the computer does it, you're no longer validating what the user did.

          The checkbox passes the dish_name.

          // Get the order, print it and send to the database
                  foreach($_POST['od'] as $order)
          {
              $qty = $order['qty']; // this is the qty
              $sel = $order['selection'];  // this is the checkbox
          
          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())"; 
            Weedpacket;10879462 wrote:

            If you want to make sure that users do both, there's no point having the computer do it for them; if the computer does it, you're no longer validating what the user did.

            If the checkbox gets checked then the user must of checked it and if a value greater than zero is entered, then it goes to say that the user did that also. I just want to be sure that the values for both the qty as well as the dish_name get sent. If you know of a better way, then that would be greatly welcomed as well.

              I want it to be user friendly and we know most may check and not enter a qty or vice versa lol.

              You need javascript to do this. Via an "onclick" for the checkbox...

              <input type="checkbox" name="od['.$ix.'][selection]" value="' . $row['dn'] .'" onclick="somejavascriptfunction(this)">
              <script type="text/javascript">
              function somejavascriptfunction(o) {
              	if (o.checked) {
              		//do something
              	}
              }
              </script>
              

              You can do the same thing for an onchange event for the text input for quantity.

                bretticus;10879469 wrote:

                You need javascript to do this. Via an "onclick" for the checkbox...

                <input type="checkbox" name="od['.$ix.'][selection]" value="' . $row['dn'] .'" onclick="somejavascriptfunction(this)">
                <script type="text/javascript">
                function somejavascriptfunction(o) {
                	if (o.checked) {
                		//do something
                	}
                }
                </script>
                

                You can do the same thing for an onchange event for the text input for quantity.

                I am stumped...been working on it and reading on it since this reply. So far all I can get without getting errors is this and it doesn't change the value but at least no errors:

                <script language="javascript" type="text/javascript">
                function setvalue(checked) {
                
                if document.menu.od.selection.checked=true {
                document.menu.od.quantity.value == 1;
                }
                }
                </script>

                Seems the more I read the more confused I get. Or I'm just too tired and need to clear my head.

                  TheQ;10879491 wrote:

                  I am stumped...been working on it and reading on it since this reply. So far all I can get without getting errors is this and it doesn't change the value but at least no errors

                  Well, you can pass the object (checkbox you click on) using the "this" keyword as I showed in my example. What you are going to have to do though is set an id in your checkbox that corresponds in some manner to the corresponding text field.

                  <script language="javascript" type="text/javascript">
                  function setvalue(cb) {
                  
                  if (cb.checked) {
                  document.getElementById(cb.id+'_qty').value = 1;
                  }
                  }
                  </script>

                  You are going to have to change the id field for your text input OR make the id of the checkbox the same with a slight variation. THE POINT BEING, you need someway to stipulate which text input you are going to set a value for. The code above is an example and will not work with your present code.

                  Also, if you are new to javascript, and since this is a PHP forum, you might get better help on a javascript forum.

                  Just a hint. Javascript is the same as php as far as setting a variable or checking for equailty. For example, to check if a variable is equal to (or less than, etc.) another variable you would use ==:

                  var a=1,b=2
                  if (a==b){
                  alert('equal');
                  }

                  However, notice that I set the variables with a single equals sign (=)

                  var a=1,b=2
                  if (a!=b){
                  a=b;
                  }

                    Thanks for your reply and I've posted the javascript question in another forum.

                    But, after sleeping on it, I am looking at it in another way now.

                    What if I remove the checkbox from the form. All I'm really wanting to be sure to do is send the dish_name value to the database based on it being ordered.

                    Here's what I did:

                    	
                    	// 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="text" name="od['.$ix.'][qty]" id="od_' .
                            $row['d1'] . '_' . $row['price'] . '" size="2" value="" onchange="CalculateTotal(this.form)"></td>';
                    
                        <td align="left"><input type="hidden" name="od['.$ix.'][selection]" value="' . $row['dn'] .'">' . $row['dn'] . '</td> 
                    // added a hidden input (the same as the checkbox) to be passed to the db
                            <td align="left">' . $row['dd'] . '</td>
                            <td align ="right">' . $row['price'] . '</td></tr>';
                    	$ix++; // increment index
                    	}
                    	

                    It works great. Thanks for all input and help!

                      Write a Reply...