theredking Posted September 3, 2007 Share Posted September 3, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/67781-echo-multiple-results-from-checkboxes/ Share on other sites More sharing options...
Daniel0 Posted September 3, 2007 Share Posted September 3, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/67781-echo-multiple-results-from-checkboxes/#findComment-340544 Share on other sites More sharing options...
theredking Posted September 3, 2007 Author Share Posted September 3, 2007 Ok thanks. I have tried this, and it echoes "array" instead of the value of the checkboxes... any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/67781-echo-multiple-results-from-checkboxes/#findComment-340564 Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Share Posted September 3, 2007 doe foreach ($_GET['lang'] as $value) { echo $value; } I THINK My syntax is correct. Quote Link to comment https://forums.phpfreaks.com/topic/67781-echo-multiple-results-from-checkboxes/#findComment-340568 Share on other sites More sharing options...
Daniel0 Posted September 3, 2007 Share Posted September 3, 2007 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). Quote Link to comment https://forums.phpfreaks.com/topic/67781-echo-multiple-results-from-checkboxes/#findComment-340570 Share on other sites More sharing options...
theredking Posted September 3, 2007 Author Share Posted September 3, 2007 Ah, ok. I am sending via POST though, what options does that leave me with? Quote Link to comment https://forums.phpfreaks.com/topic/67781-echo-multiple-results-from-checkboxes/#findComment-340573 Share on other sites More sharing options...
wildteen88 Posted September 3, 2007 Share Posted September 3, 2007 Change $_GET['lang'] to $_POST['lang'] instead for d22552000's code Quote Link to comment https://forums.phpfreaks.com/topic/67781-echo-multiple-results-from-checkboxes/#findComment-340575 Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Share Posted September 3, 2007 yep My code may not be small, but it is simple and to the point. I dont understnad print -,- Quote Link to comment https://forums.phpfreaks.com/topic/67781-echo-multiple-results-from-checkboxes/#findComment-340578 Share on other sites More sharing options...
theredking Posted September 3, 2007 Author Share Posted September 3, 2007 Oh ok, cool thank you! Quote Link to comment https://forums.phpfreaks.com/topic/67781-echo-multiple-results-from-checkboxes/#findComment-340579 Share on other sites More sharing options...
theredking Posted September 3, 2007 Author Share Posted September 3, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/67781-echo-multiple-results-from-checkboxes/#findComment-340627 Share on other sites More sharing options...
Daniel0 Posted September 3, 2007 Share Posted September 3, 2007 What's your HTML? Quote Link to comment https://forums.phpfreaks.com/topic/67781-echo-multiple-results-from-checkboxes/#findComment-340630 Share on other sites More sharing options...
theredking Posted September 3, 2007 Author Share Posted September 3, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/67781-echo-multiple-results-from-checkboxes/#findComment-340645 Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Share Posted September 3, 2007 lol it should work/ Quote Link to comment https://forums.phpfreaks.com/topic/67781-echo-multiple-results-from-checkboxes/#findComment-340647 Share on other sites More sharing options...
Daniel0 Posted September 3, 2007 Share Posted September 3, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/67781-echo-multiple-results-from-checkboxes/#findComment-340651 Share on other sites More sharing options...
theredking Posted September 3, 2007 Author Share Posted September 3, 2007 Ok, I don't know what I did, but I tried it again and it worked. Thanks for your time looking at this. Quote Link to comment https://forums.phpfreaks.com/topic/67781-echo-multiple-results-from-checkboxes/#findComment-340663 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.