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
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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.