Page 1 of 1

New to PHP! Form not processing check boxes

Posted: Fri Mar 27, 2009 1:28 pm
by godleuf
I am going to first display the checkbox code from the form itself:

Code: Select all

 
<input type="checkbox" value="Please send me a Travel Planner" name="options[]">
<input type="checkbox" value="Please send me a Visitor Map" name="options[]" />
<input type="checkbox" value="Please sign me up for the email newsletter" name="options[]" />
 
Now the form that processes it:

Code: Select all

 
<?php
// ------------- CONFIGURABLE SECTION ------------------------
 
// $mailto - set to the email address you want the form
// sent to, eg
//$mailto       = "youremailaddress@example.com" ;
 
$mailto = 'xxxxx@xxxxxxxxx.com' ;
 
// $subject - set to the Subject line of the email, eg
//$subject  = "Feedback Form" ;
 
$subject = "Request For Visitor Guide" ;
 
// the pages to be displayed, eg
//$formurl      = "http://www.example.com/feedback.html" ;
//$errorurl     = "http://www.example.com/error.html" ;
//$thankyouurl  = "http://www.example.com/thankyou.html" ;
 
$formurl = "http://www.example.com/requestform_mtg.php" ;
$errorurl = "http://www.example.com/error.php" ;
$thankyouurl = "http://www.example.com/thankyou.php" ;
 
$email_is_required = 1;
$name_is_required = 1;
$address_is_required = 1;
$contactname_is_required = 1;
$city_is_required = 1;
$zip_is_required = 1;
$phone_is_required = 1;
$uself = 0;
$use_envsender = 0;
$use_webmaster_email_for_from = 1;
$use_utf8 = 1;
 
// -------------------- END OF CONFIGURABLE SECTION ---------------
 
$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
$content_type = (!isset( $use_utf8 ) || ($use_utf8 == 0)) ? 'Content-Type: text/plain; charset="iso-8859-1"' : 'Content-Type: text/plain; charset="utf-8"' ;
if (!isset( $use_envsender )) { $use_envsender = 0 ; }
$envsender = "-f$mailto" ;
$name = $_POST['name'] ;
$contactname = $_POST['contactname'] ;
$title = $_POST['title'] ;
$email = $_POST['email'] ;
$address = $_POST['address'] ;
$city = $_POST['city'] ;
$state = $_POST['state'] ;
$zip = $_POST['zip'] ;
$fax = $_POST['fax'] ;
$phone = $_POST['phone'] ;
$mtgname = $_POST['mtgname'] ;
$dates = $_POST['dates'] ;
$attendance = $_POST['attendance'] ;
$guestroom = $_POST['guestroom'] ;
$mtgroom = $_POST['mtgroom'] ;
$timeframe = $_POST['timeframe'] ;
$options = $_POST['options'] ;
$comments = $_POST['comments'] ;
$http_referrer = getenv( "HTTP_REFERER" );
 
if (!isset($_POST['email'])) {
    header( "Location: $formurl" );
    exit ;
}
if (($email_is_required && (empty($email) || !ereg("@", $email))) || ($name_is_required && empty($name)) || ($address_is_required && empty($address)) || ($contactname_is_required && empty($contactname)) || ($city_is_required && empty($city)) || ($zip_is_required && empty($zip)) || ($phone_is_required && empty($phone))) {
    header( "Location: $errorurl" );
    exit ;
}
if ( ereg( "[\r\n]", $name ) || ereg( "[\r\n]", $email ) || ereg( "[\r\n]", $address ) || ereg( "[\r\n]", $contactname ) ) {
    header( "Location: $errorurl" );
    exit ;
}
if (empty($email)) {
    $email = $mailto ;
}
$fromemail = (!isset( $use_webmaster_email_for_from ) || ($use_webmaster_email_for_from == 0)) ? $email : $mailto ;
 
if (get_magic_quotes_gpc()) {
    $comments = stripslashes( $comments );
}
 
$messageproper =
    "This message was sent from:\n" .
    "$http_referrer\n" .
    "------------------------------------------------------------\n" .
    "Organization Name: $name\n" .
    "Contact Name: $contactname\n" .    
    "Email of sender: $email\n" .
    "Address of sender: $address\n" .
    "City of sender: $city\n" .
    "State of sender: $state\n" .
    "Zip Code of sender: $zip\n" .  
    "Fax of sender: $fax\n" .   
    "Phone of sender: $phone\n" .
    "Meeting Name: $mtgname\n" .            
    "Preferred Dates: $dates\n" .           
    "Expected Attendance: $attendance\n" .          
    "Guest Rooms: $guestroom\n" .
    "Largest Meeting Room Needed: $mtgroom\n" .             
    "Decision Timeframe: $timeframe\n" .    
    "Options: $options\n" .                             
    "------------------------- COMMENTS -------------------------\n\n" .
    $comments .
    "\n\n------------------------------------------------------------\n" ;
$headers =
    "From: \"$name\" <$fromemail>" . $headersep . "Reply-To: \"$name\" <$email>" . $headersep . "X-Mailer: chfeedback.php 2.13.0" .
    $headersep . 'MIME-Version: 1.0' . $headersep . $content_type ;
 
if ($use_envsender) {
    mail($mailto, $subject, $messageproper, $headers, $envsender );
}
else {
    mail($mailto, $subject, $messageproper, $headers );
}
header( "Location: $thankyouurl" );
exit ;
 
?>
 
Why do the checkboxes show up in my email as "array"?

Thanks.

Re: New to PHP! Form not processing check boxes

Posted: Fri Mar 27, 2009 2:56 pm
by califdon
Because it is an array. This does get somewhat confusing, and actually I never knew that you can create an array of checkbox names directly in HTML in that manner--I had to test it myself when I read your post. Here's my little test script, which should explain how you address each array element in the PHP script. Save this as a file named testboxes.php and try it:

Code: Select all

<html>
<head><title></title></head>
<body>
<?php
if(isset($_POST['test'])) {
   $box1= isset($_POST['boxes'][0]) ? "Yes" : "No";
   $box2= isset($_POST['boxes'][1]) ? "Yes" : "No";
   echo "Boxes checked: #1 - $box1, #2 - $box2<br/>";
   echo "<input type='button' value='Return' onClick='window.location=\"testboxes.php\";'>";
} else {
   echo "<form method='post' action='testboxes.php'>";
   echo "<input type='text' name='test' value='TEST'><br/>";
   echo "<input type='checkbox' name='boxes[]'> Box 1<br/>";
   echo "<input type='checkbox' name='boxes[]'> Box 2<br/>";
   echo "<input type='submit' value='Submit'></form>";
}
?>
</body>
</html>

Re: New to PHP! Form not processing check boxes

Posted: Sat Mar 28, 2009 6:55 pm
by becky-atlanta
Here is an example of what I did with checkboxes:

Example of the form:

Code: Select all

 
<input name="week[]" type="checkbox" id="week" value="1 - Monday, June 22 - Friday, June 26">
<input name="week[]" type="checkbox" id="week" value="2 - Monday, July 29 - Friday, July 3 (half day)">
<input name="week[]" type="checkbox" id="week" value="3 - Monday, July 6 - Friday, July 10 ">
<input name="week[]" type="checkbox" id="week" value="4 - Monday, July 13 - Friday, July 17">
<input name="week[]" type="checkbox" id="week" value="5 - Monday, July 20 - Friday, July 24">
<input name="week[]" type="checkbox" id="week" value="5 - Monday, July 20 - Friday, July 24">
 
To process checkboxes:

Code: Select all

 
foreach ($_POST["week"] as $wk) {
echo $wk;
echo "<br>\n";}
 
I hope this helps.