1

Thank you for looking at my question.

I am trying to do this: Use a form with checkboxes: user clicks on several checkboxes (for ONE question). Then hits "submit" Then a PHP page takes the form elements and puts them into variables. I can do this with other data, but not sure how to do it with checkboxes (since it is an array).

Could you help me?

$Q1 = $_POST['Hood'];  // This one is the array. Let's say it's an array that holds 3 words (red, white, blue). I'm fine if they all get stored in $Q1, I just don't know how to loop through the array (in $POST['Hood']) to get all 3 words in that one variable ($Q1)

$Q2 = $_POST['Handle'];
$Q3 = $_POST['Lever'];

So I think I need to figure out how to loop through $_POST['Hood'] in order to get each array element out of it. Please help :)

Thank you for your assistance!

1 Answer 1

2

Here is how your form input must look like and the processing part in PHP. Note the input name has an array initialization:

echo '<form method="post">
<input type="checkbox" name="Hood[]" value="some value"> some value<br>
<input type="checkbox" name="Hood[]" value="some other value"> some other value<br>
<input type="checkbox" name="Hood[]" value="1"> a number<br>
<button type="submit">Submit</button>
</form>';

if(isset($_POST['Hood'])) {

    foreach($_POST['Hood'] as $key=>$value) {

        echo $value;

    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Great, I am going to try it now!

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.