Hi all,

I have a form which is created by the results of a SQL query from table1. Depending on the result of the query, say 20 rows, then the form will have 20 rows (20 Questions and 20 checkbox).

Form part:

<textarea name="audititem[]"><?php echo $row_InspectItems['AuditItem']; ?></textarea>
<input type="hidden" name="audititemid[]" id="hiddenField" value="<?php echo $row_InspectItems['RecordID']; ?>">
 <input name="check[]" type="checkbox" class="checkbox" value="<?php echo $row_InspectItems['RecordID']; ?>">

Process Part:

$Questions[] = $_POST['audititem'];
$Check[] = $_POST['check'];
foreach($_POST['audititem'] as $key => $question) { 
    $check = $_POST['check'][$key];
    $query = mysql_query("INSERT INTO table2 (AuditItem, AuditCheck) VALUES ('{$question}','{$check }')"); 
}

The problem I have is when the data is inserted to table2 the matching question and checkbox value do not get written to the correct feilds.

Example:

If (form)
question1 checkbox is selected
question2 checkbox is selected
question15 checkbox is selected

the data gets written as

Feild Question1 "question1" Feild Question1 checkbox 1
Feild Question2 "question2" Feild Question2 checkbox 2
Feild Question15 "question15" Feild Question3 checkbox 3

I think my question is how do I keep the question matched to the checkbox result.

    Here's what I'd do:

    1. Remove the hidden form field altogether. It doesn't really do you any good (reliably, at least). Who ever said that the client's browser has to submit the textareas and the hidden form fields in the exact same order?

    2. Insert the 'Record_ID' value inside the square brackets for the textarea name. This way, $POST['audititem'] will be an array where the key is the Record_ID value (in other words, the ID is directly paired with the corresponding value from the form).

    3. Do the same as in #2 for the checkbox fields. At this point, the "value" of the checkbox becomes meaningless - you could just set it to a static "1" or "I like watermelons." You can then loop over $_POST['check'] (if it's a non-empty array) in the same manner, except you'd really only care about the 'key' values (which would correspond to the Record_ID of the rows that were checked).

      I agree with bradgrafelman on all points; remember that unchecked checkboxes are not submitted.

        Hi Brad,

        Not working corrently. The content of the "checkbox" is getting inserted to the"question" feild and the "checkbox" feild is empty.

        This is what I have changed;

        The form:

        <input name="itemno" type="text"  class="audititemsno" id="itemno" value="<?php print $row_InspectItems['AuditItemNo']; ?>" >
        <textarea name="audititem[<?php echo $row_InspectItems['RecordID']; ?>]"><?php echo $row_InspectItems['AuditItem']; ?></textarea>
        <input name="check['.<?php echo $row_InspectItems['RecordID']; ?>.']" type="checkbox" class="checkbox" />
        

        The process

        foreach($_POST['check'] as $key => $question) { 
            $check = $_POST['check'][$key];
        

        I know this is going to be something simple but I can't see where the wrong code is.

        Many thanks for looking at this.

          Inside your foreach() loop, all you need is $key. That's the Record_ID of the checked checkbox that you're currently processing. If you want the corresponding 'question' value, then you'd use that same value as the key for the $_POST['question'] array.

            You'd also want to loop over some other array, unless you want to ignore unchecked entries.

              Weedpacket;11023695 wrote:

              You'd also want to loop over some other array, unless you want to ignore unchecked entries.

              Going back to the first post, you might even want to consider not looping over the checkbox array at all. For example, if the desire is to execute a SQL query for all of the 'question' values in the form as well as indicating whether or not the corresponding checkbox was checked, then you wouldn't want to loop over the checkbox array.

              Instead, you'd want to loop over the 'question' array; to check if its checkbox was checked, you'd simply check if the Record_ID exists as a key in the checkbox array (e.g. by using [man]isset/man or [man]array_key_exists/man).

                Hi Brad,

                My am I struggling with this.

                I now now managed to insert the question in to the table but the checkbox feild gets populated with the RecordID which is half way there. The problem is that all the checkboxes are populated not just the ones that were checked.

                foreach($_POST['question'] as $key => $question) {  
                $check = $_POST['check'][$key]; $ans = $_POST['question'][$key];

                I am using $ans content for the question, the insert is correct.
                And I am using $key for the checkbox feild.

                  This:

                  $check = $_POST['check'][$key];

                  is either going to be whatever static/useless value you assigned to the checkboxes, or it's going to generate an error message because the checkbox wasn't checked, thus $_POST['check'][$key] doesn't exist.

                  Instead, you'd want something like:

                  $check = isset($_POST['check'][$key]);

                  This would tell you whether or not the checkbox with the ID of $key (which is the Record_ID of the current question being processed) was checked.

                    Hi Brad,

                    Sorry its me again.

                    The problem is the $POST['check'][$key], if I echo out the content of "$POST['check']" - print_r $_POST['check'] - I get the following:

                    Array ( ['.1.'] => 1 ['.2.'] => 1 ['.3.'] => 1 ['.4.'] => 1 ['.19.'] => 1 ['.20.'] => 1 )

                    This is with questions 1 to 4, 19 and 20 checked. The content of the array looks correct.

                    Using

                    foreach($_POST['question'] as $key => $question) {  
                    
                    $ans = $_POST['question'][$key];
                    print $ans;
                    
                    $check = isset($_POST['check'][$key]);  
                    print $check;
                    
                    }
                    

                    and then echo out the content of $check, it appears empty.

                    Can you spot what is going wrong.

                      Hi Brad,

                      Found the problem.

                      The foreach statement was correct, it was the form feild which was not constructed correctly.

                      This is how it was

                      <input name="check['.<?php echo $row_InspectItems['RecordID']; ?>.']" type="checkbox" class="checkbox" value="1" />
                      

                      by removing the "'." after and before the [] brackets it now works.

                      This is how it is now working

                      <input name="check[<?php echo $row_InspectItems['RecordID']; ?>]" type="checkbox" class="checkbox" value="1" />
                      

                      Thanks for all your help with this. You are the man.

                        Write a Reply...