1

I tried combining two answers from here: counting how many checkbox are checked php/html And here: validating input types "checkbox" and "number" php/html

My goal is to count how many checkboxes were selected, and insert their values along with the number value in the number input.

I found various other posts about it, but couldn't figure out an answer.

Here is the code:

echo '<form action="" method="post">';

Then I have a for loop, where I create my table rows, with the checkboxes. Where $id = $row[0] is updated on each loop, which is the id equivalent of my entry. (The output of

echo '<td style="vertical-align: top;">' . $row[0] . '</td>';

Shows the id correctly)

echo '<tr>';
echo "<td style='vertical-align: top;'><input type='checkbox' name='choice[$id][id]' value='$id'></td>";
echo "<td style='vertical-align: top;'><input type='number' name='choice[$id][order]' size='20'></td>";
echo '</tr>';
echo '<b> <input type="submit" value="Insert"></b>';

Then, on the next page, I have this:

$var_checkbox=$_POST['choice'];
$sql_var_id = "SELECT id FROM custom_form WHERE id=(SELECT max(id) FROM custom_form)";
$var_id_result = mysqli_query($link,$sql_var_id);
$var_id = mysqli_fetch_array($var_id_result);

        $count=count($var_checkbox[$var_id[0]]);

        for($i=0; $i<$count; $i++){
            if($var_checkbox[$i]!= NULL){

                $sql1 = sprintf("INSERT INTO custom_form_has_property (custom_form_id,property_id,field_order) VALUES ('%d','%d','%d');",
                    $var_id[0],
                    $var_checkbox[$var_id[0]][id],
                    $var_checkbox[$var_id[0]][order]
                );
                $result1 = mysqli_query($link,$sql1);
            }
        }

The problem is, that no values of checkboxes or numbers are inserted.

(As a side note, to try and be more specific) If instead of matrixes, I use

   echo "<td style='vertical-align: top;'><input type='checkbox' name='choice[]' value='$row[0]'></td>";
   echo '<td style="vertical-align: top;"><input type="number" name="order[]" size="2"></td>';

And

    $var_checkbox=$_POST['choice'];
    $order = $_POST['order'];

Then

echo "Orders: $order[0],$order[1],$order[2],$order[3],$order[4],$order[5],$ordemr[6]";
echo "Choices: $var_checkbox[0],$var_checkbox[1],$var_checkbox[2],$var_checkbox[3],$var_checkbox[4],$var_checkbox[5],$var_checkbox[6]";

Will output

Orders: 1,,2,,3,,Choices: 1,3,13,,,,

I need the choiced from the checkboxes to be somehow linked to the orders from the number field. And the only way to achieve that, is if they have the same name, but different indexes, which is why I have the matrix, in the first index, it's saved the id number, which is generated on each loop, and in the second, the actual name, which makes the distinction between them (id and order).

I tried various other things, but it all comes back to the same problem... The value of order (number) is stored into the array, even when null, while the value of choices (checkbox) doesnt. I could do an array_filter(), but there is no guarantee that the user will not input an order, while not ticking a choice checkbox. If I would compare the length of choice with the length of order, after filtered, and if they output the same size, there is still no guarantee, that the user didnt check checkbox of line x, and added a value in the order field, on line y.

Maybe there is a way to pass unchecked values to the choice[] variable as null, the same way it happens in order[]?

10
  • You're presently mixing MySQL APIs mysql_ and mysqli_ which do not intermix with each other. You're looking to use mysqli_real_escape_string() rather than mysql_real_escape_string(), where mysqli_real_escape_string() requires DB connection be passed as the first parameter. Commented Jan 21, 2015 at 4:00
  • @Fred, he doesn't need to escape those at all, because they are numbers (see %d?). Commented Jan 21, 2015 at 4:01
  • I know, I have tried with both, but I had bad experiences with mysqli_real_escape_string(). Commented Jan 21, 2015 at 4:02
  • Either way, if you're going to want to use real_escape_string(), you can't intermix APIs which is what you are doing right now. Commented Jan 21, 2015 at 4:03
  • 1
    What is this? choice[.$id.] ? what are the dots for? I believe this is the bug! Commented Jan 21, 2015 at 4:16

2 Answers 2

0

What is this? choice[.$id.] ? what are the dots for?

I believe this is the bug!
You probably were trying to concatenate $id into the string before and just forgot to remove them?

Corrected line:

echo "<td style='vertical-align: top;'><input type='checkbox' name='choice[$id][id]' value='$id'></td>";
Sign up to request clarification or add additional context in comments.

1 Comment

This is the output of echo "Choices: $var_checkbox[1][id],$var_checkbox[1][order] (also removed the dots): "Choices: Array[id],Array[order]"
0

there are some way to do this ..

  1. you have to count total no of selected check box via jQuery and set those value in hidden field at view page on each check box check or on form submit

  2. you have to take counter on insert part and save the ids and at last update ids value with counter value in database ....

6 Comments

Yes, I was thinking about something similar... Count the total numbers of rows. Add the value via a hidden input. Then for each loop, until i < count, check if the checkbox has been selected, and if yes, then add the values to the DB. However, I think this overcomplicates things...
why u use i<count ?? you run loop for total no of check box then directly use counter (hidden field value) for store total no of check check box....
and the checkbox name "choice[]" will only post into the array the values of the checked boxes.
so you have to just count choice[] it will give only total no of selected check box and store that value ....
Currently, I only count the boxes that are checked. The problem is that the input type number, with name order[] will output an array that has both the null values and the entered values
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.