Jump to content

returning multiple checkbox results


KittyR

Recommended Posts

Hi,

 

I've been banging my head trying to figure this out... I would appreciate any help.  :)

 

I'm working on a form that has questions with multiple-answer checkboxes.

 

I have tried this:

 

Form html

Inquiry A: <br />
<input name="inquiry_a" type="checkbox" value="answer_1"></input> Answer 1 <br />
<input name="inquiry_a" type="checkbox" value="answer_2"></input> Answer 2 <br />
[and so on]

 

corresponding php file code

if ($inquiry_a == "answer_1") {
$inq1 = "Answer 1 \r";
}
if ($inquiry_a == "answer_b") {
$inq2 = "Answer 2 \r";
}
[and so on]

$inq = $inq1 . $inq2 . [and so on];

 

Then I ask it to return $inq to me in an email, expecting that I will see all checked answers with no blank lines.

 

However, no matter what is checked, only the last answer in the list is returned.  But have this same code structure in another form on the same web host and it works fine.  What am I missing?

 

I'm sorry, I'm not sure at the moment what version of php I have installed, but if that makes a big difference I will try to find out.

 

thanks,

Kitty

Link to comment
https://forums.phpfreaks.com/topic/36788-returning-multiple-checkbox-results/
Share on other sites

You have to create an array regarding the answers/boxes checked. Like this:

 

<input name="inquiry_a[]" type="checkbox" value="answer_1"></input> Answer 1 <br />

<input name="inquiry_a[]" type="checkbox" value="answer_2"></input> Answer 2 <br />

 

With the brackets. That establishes the array called 'inquiry_a' for which you can then manipulate each response.

Hi and thanks for your response.

 

Could you please tell me what I need to change in the PHP code to go along with this, or direct me to the proper tutorial section?  Because now I get nothing returned no matter what boxes are checked.

 

thanks again,

Kitty

UPDATE:

 

I tried this:

 

HTML...

Inquiry A: <br />
<input name="inquiry_a[]" type="checkbox" value="answer_1"></input> Answer 1 <br />
<input name="inquiry_a[]" type="checkbox" value="answer_2"></input> Answer 2 <br />
[and so on]

 

php...

$response1 = $_POST['inquiry_a'];

 

then asked it for...

Inquiry A: \r
$response1 \n

 

All I get back is:

 

Inquiry A

Array

 

???

It says 'Array' because thats what it is - a variable that can hold a set of values, if you want to see its values you can use print_r() to give you a rough dump of whats in an array or use a loop to go through each value stored in the array. If its multi choice you might be better of with radio buttons so that only one answer can be provided. Heres some basic code you can play around with if you like, try changing type="radio" to type="checkbox" and you'll see that the code still works.

<html><body>

<?php

//print_r($_POST['answer']); - this gives you a basic dump of what is in the array

//loop through each provided answer
if(isset($_POST['answer']) and !empty($_POST['answer'])) {
    foreach($_POST['answer'] as $an_answer) {
        echo "The answer You chose was ".$an_answer."<br/>";
    }
}
?>

<form method="post" action="">
1<input type="radio" name="answer[]" value="1" /><br/>
2<input type="radio" name="answer[]" value="2" /><br/>
3<input type="radio" name="answer[]" value="3" /><br/>
4<input type="radio" name="answer[]" value="4" /><br/>
<input type="submit"/>
</form>
</body></html>

 

Some reference

http://au.php.net/manual/en/language.types.array.php

http://au.php.net/manual/en/language.control-structures.php

Well, I'm very close....

 

Yes, I would prefer radio boxes but it does have to be checkboxes because the user might have more than one selection for the question.

 

All selections are echoing onto the screen after I click the Submit button, but the email which comes to me only shows the last box checked.

 

[sigh]

 

I'll keep banging my head against it unless someone has an idea of what to try next.

 

thanks!

Kitty

Got it!

 

I just need to gather the checkbox values into a variable, and I didn't know I needed the dot:

 

foreach($_POST['answer'] as $answer_var) {
$answer_var .= "$ans1";
}

 

Then I ask for $ans1 /n to be emailed to me, and they're all listed.  ;D

 

Thanks for your help.

 

Kitty

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.