Jump to content

echo multiple results from checkboxes...


theredking

Recommended Posts

I'm making great progress with my first real php form processing project, but I've come a little unstuck.

 

I'm trying to echo the results from a checkbox. Currently I have

<?php echo "$checkbox"; ?>

 

This works, but if multiple selections are made, it only echoes one of them. How do I fix this?

 

Thank you in anticipation!

 

 

Link to comment
https://forums.phpfreaks.com/topic/67781-echo-multiple-results-from-checkboxes/
Share on other sites

Name them like this:

Which of the following languages do you speak?<br />
<input type='checkbox' name='lang[]' value='en' /> English<br />
<input type='checkbox' name='lang[]' value='es' /> Spanish<br />
<input type='checkbox' name='lang[]' value='fr' /> French

 

Then you can access it like $_POST['lang'] or $_GET['lang] which will be an array containing the selected languages.

Ok thanks. I have tried this, and it echoes "array" instead of the value of the checkboxes... any ideas?

 

That because (as I said) it is an array.

 

Do

print_r($_GET['lang']);

to see and you can use d22552000's code to iterate through it (providing you send it via GET).

Ok, so I got that working, but... I have another instance of checkboxes in the same form, which I want to do the same thing for.

 

I tried to use my logic and just do:

foreach ($_POST['interest'] as $value2) {
echo "$value2 " ;

 

but that didn't work...

 

Any ideas?

Is this what you're asking for?

 

<input name="interest[]" type="checkbox" id="interest[]" value="Fishing" />Fishing 
<input name="interest[]" type="checkbox" id="interest[]" value="Golf" /> Golf 
<input name="interest[]" type="checkbox" id="interest[]" value="Tennis" /> Tennis 
<input name="interest[]" type="checkbox" id="interest[]" value="Swimming" /> Swimming

Is this what you're asking for?

 

<input name="interest[]" type="checkbox" id="interest[]" value="Fishing" />Fishing 
<input name="interest[]" type="checkbox" id="interest[]" value="Golf" /> Golf 
<input name="interest[]" type="checkbox" id="interest[]" value="Tennis" /> Tennis 
<input name="interest[]" type="checkbox" id="interest[]" value="Swimming" /> Swimming

 

ids must be unique. That's not the problem though. It should work. Try print_r($_POST); on the page which your form leads to.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.