Form with Checkbox Array Only Returning Last Result

Hi all-

I’m new to PHP and I’m having issues with a form I’ve been working on that includes checkboxes as an array. The form mostly works- everything emails back to me just fine and the thank you page redirects as it should. The only problem is that, if I check more than one box (as I’m expecting most users will), it only returns the results of the last box I checked.

I’m not sure how much code you need, so I’ll give what I think would be helpful. Let me know if you need more. Any help is MUCH appreciated. THANK YOU IN ADVANCE!

HTML:

[code]



All fields with an asterisk are required.

		<table width="500" border="0" cellspacing="0" cellpadding="0">
		  <tr>
		    <td><label for="name"><small>Full Name*</small></label></td>
		    <td><input type="text" name="name" id="name" value="" size="40" /></td>
		  </tr>
		  <tr>
		    <td><label for="address"><small>Address*</small></label></td>
		    <td><input type="text" name="address" id="address" value="" size="40" /></td>
		  </tr>
		  <tr>
		    <td><label for="city"><small>City*</small></label></td>
		    <td><input type="text" name="city" id="city" value="" size="40" /></td>
		  </tr>
		  <tr>
		    <td><label for="state"><small>State*</small></label></td>
		    <td><input type="text" name="state" id="state" value="" size="40" /></td>
		  </tr>
		  <tr>
		    <td><label for="zip"><small>Zip*</small></label></td>
		    <td><input type="text" name="zip" id="zip" value="" size="40" /></td>
		  </tr>
		  <tr>
		    <td><label for="phone"><small>Phone</small></label></td>
		    <td><input type="text" name="phone" id="phone" value="" size="40" /></td>
		  </tr>
		  <tr>
		    <td><label for="email"><small>Email*</small></label></td>
		    <td><input type="text" name="email" id="email" value="" size="40" /></td>
		  </tr>
		  <tr>
		    <td>I will do the following:</td>
		    <td><input name="volunteer" type="checkbox" value="Vote for XXX in November" />Vote for XXX in November<br />
			<input name="volunteer" type="checkbox" value="Put a bumper sticker on my car" />Put a bumper sticker on my car<br />
			<input name="volunteer" type="checkbox" value="Put a yard sign at my home" />Put a yard sign at my home<br />
			<input name="volunteer" type="checkbox" value="Talk with my neighbors" />Talk with my neighbors<br />
			<input name="volunteer" type="checkbox" value="Host a Meet-and-Greet at my home" />Host a Meet-and-Greet at my home<br />
			<input name="volunteer" type="checkbox" value="Volunteer at XXX's headquarters" />Volunteer at XXX's headquarters<br />
			<input name="volunteer" type="checkbox" value="Donate to XXX's campaign" />Donate to XXX's campaign<br /></td>
		  </tr>
		</table>
		<br />
	  <p>
	    <input name="submit" type="submit" id="submit" value="Submit Form" />
	    &nbsp;
	    <input name="reset" type="reset" id="reset" value="Reset Form" />
	  </p>
	</form>[/code]

And the PHP:
[php]<?php
// First, make sure the form was posted from a browser.
// For basic web-forms, we don’t care about anything
// other than requests from a browser:
if(!isset($_SERVER[‘HTTP_USER_AGENT’])){
die(“Forbidden - You are not authorized to view this page”);
exit;
}

// Make sure the form was indeed POST’ed:
// (requires your html form to use: action=“post”)
if(!$_SERVER[‘REQUEST_METHOD’] == “POST”){
die(“Forbidden - You are not authorized to view this page”);
exit;
}

// Host names from where the form is authorized
// to be posted from:
$authHosts = array(“domain.com”);

// Where have we been posted from?
$fromArray = parse_url(strtolower($_SERVER[‘HTTP_REFERER’]));

// Test to see if the $fromArray used www to get here.
$wwwUsed = strpos($fromArray[‘host’], “www.”);

// Attempt to defend against header injections:
$badStrings = array(“Content-Type:”,
“MIME-Version:”,
“Content-Transfer-Encoding:”,
“bcc:”,
“cc:”);

// Loop through each POST’ed value and test if it contains
// one of the $badStrings:
foreach($_POST as $k => $v){
foreach($badStrings as $v2){
if(strpos($v, $v2) !== false){
logBadRequest();
header(“HTTP/1.0 403 Forbidden”);
exit;
}
}
}

// Made it past spammer test, free up some memory
// and continue rest of script:
unset($k, $v, $v2, $badStrings, $authHosts, $fromArray, $wwwUsed);

$name = $_POST[‘name’] ;
$address = $_REQUEST[‘address’] ;
$city = $_REQUEST[‘city’] ;
$state = $_REQUEST[‘state’] ;
$zip = $_REQUEST[‘zip’] ;
$phone = $_REQUEST[‘phone’] ;
$email = $_REQUEST[‘email’] ;
$volunteer = $_POST[‘volunteer’] ;

$Body = "<b>Contact Information Below:</b><br />";
$Body .= "\n";
$Body .= "<b><font size 1>Name: </font></b>";
$Body .= $name;
$Body .= "\n<br />";
$Body .= "<b><font size 1>Address: </font></b>";
$Body .= $address;
$Body .= "\n<br />";
$Body .= "<b><font size 1>City: </font></b>";
$Body .= $city;
$Body .= "\n<br />";
$Body .= "<b><font size 1>State: </font></b>";
$Body .= $state;
$Body .= "\n<br />";
$Body .= "<b><font size 1>Zip: </font></b>";
$Body .= $zip;
$Body .= "\n<br />";
$Body .= "<b><font size 1>Phone: </font></b>";
$Body .= $phone;
$Body .= "\n<br />";
$Body .= "<b><font size 1>Email: </font></b>";
$Body .= $email;
$Body .= "\n<br />";
$Body .= "<b><font size 1>I will do the following: </font></b>";
$Body .= $volunteer;
$Body .= "\n<br />";

mail("[email protected]", “XXXXX Volunteer Form Submission”, $Body, “From: $_REQUEST[email]\r\nContent-type: text/html; charset=utf-8”, “-f”.$_REQUEST[email]);

if ($_POST[“name”]) {

do something

header(“Location: thanks.html”);
exit;
}

?>

[/php]

in your form where you name all your checkboxes… add ‘[]’ to them: "<input name=“volunteer[]” type=“checkbox” value=“Put a bumper”

This allows you to call it as an array when you process the form.

I had actually tried that earlier, but if I do so, oddly, it just returns the result “Array”

yeah there are a couple ways to do it, you can format it a little with a foreach statement OR instead of echo $volunteers you would use print_r($volunteers); which will give you something like array ( key => value; key => value

Problem is, I’m not too slick with this stuff, so I’m doing that wrong as well. Earlier, I tried what you’re talking about with the following code:

[php] $name = $_POST[‘name’] ;
$address = $_REQUEST[‘address’] ;
$city = $_REQUEST[‘city’] ;
$state = $_REQUEST[‘state’] ;
$zip = $_REQUEST[‘zip’] ;
$phone = $_REQUEST[‘phone’] ;
$email = $_REQUEST[‘email’] ;
$volunteer = $_POST[‘volunteer’];
if (is_array($volunteer)) {
foreach ($volunteer as $key=>$val) {
echo "$key -> $val ";
} [/php]

At which point, the form stopped working entirely, wouldn’t return results, and threw the following error: Parse error: syntax error, unexpected T_IF in /home/joecoors/public_html/send_vol_email.php on line 59

(Line 59 being the line beginning with “if”)

Also true if I do this instead…

[php] $name = $_POST[‘name’] ;
$address = $_REQUEST[‘address’] ;
$city = $_REQUEST[‘city’] ;
$state = $_REQUEST[‘state’] ;
$zip = $_REQUEST[‘zip’] ;
$phone = $_REQUEST[‘phone’] ;
$email = $_REQUEST[‘email’] ;
$volunteer = $_POST[‘volunteer’];
if (is_array($volunteer)) {
}
foreach ($volunteer as $key=>$val) {
echo "$key -> $val ";
} [/php]

Anyone have any ideas? Any help would be appreciated.

If you still receive the parse error ‘unexpected T_IF’, this is probably because you’re missing some { or } and problem may be not in line #59, but above this line as well.

What is your form code now? After you submit the form, for debugging purposes, have only these few lines in your send_vol_email.php (so that you can check if your form submit data correctly):
[php]<?php
if(isset($_POST[‘submit’])){
echo ‘

’;
var_dump($_POST);
echo ‘
’;
}
?>[/php]

Okay this had me puzzled for a bit, I’ve never seen an error like this caused by such but I found the answer you have:
"$volunteer = $_POST[‘volunteer’]; "<-- delete the whitespace after the semicolon and it will work.

also the array key isn’t likely to be of much use so you might want to use something like:

[php] foreach ($volunteer as $key=>$val) {
echo $val."\n";
} [/php]

You have GOT to be kidding me. facepalm

A whitespace.

Thank you so much for you help!! You’re the hero of the day!

Glad to help! looking forward to future riddles! :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service