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" />
<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]