Question:

Modifying HTML forms using php checkbox question?

by  |  earlier

0 LIKES UnLike

I've asked this before and people didn't give the correct answer. I have a form that goes to a php validation file, and if something is wrong it sends you back to the form page, and fills in the same values you put in before submitting. On one part of this form there are multiple checkboxes that post as an ARRAY (there is more than one checkbox associated with the array variable, so a simple if(isset(variable == "blah") echo "checked"; WILL NOT WORK FOR ME). My question is, how do I get the php to check the boxes the user checked before submitting the form.

 Tags:

   Report

1 ANSWERS


  1. The only way I can think to do it would be to keep track of an individual index for each checkbox and tack it on to the end of the checkbox value eg:

    <input type="checkbox" name="check[]" value="Item 1_0" />

    <input type="checkbox" name="check[]" value="Item 2_1" />

    etc., the 0 and 1 being the indices and the _ being the string seperator. You could then test each one of the values in your POST array to see if the index number, which you can get using substr(), is the same as the value of the checkbox you're currently writing. If it is, you can simply use:

    echo "checked=\"checked\"";

    It's a bit cumbersome but it'd do the job.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions