Sending Checkbox info from php form to email

4 posts by 2 authors in: Forums > CMS Builder
Last Post: November 27, 2015   (RSS)

By JeffC - October 5, 2015

Hello,

Please could someone enlighten me on how to correctly send checkbox information from a form to an email.

In the form below I have a 5 checkboxes: Monday; Tuesday; Wednesday; Thursday; Friday. The information correctly populates the cmsB database. However, when sending the form to email it results in a php error if one or more of the days is not checked. For example, if Monday is not checked the error is Notice: Undefined index: Monday in…

I understand why this is happening (or at least I think I do). I assume that: {$_REQUEST['Monday']} should somehow include an IF statement. From here I am stumped.

Any help gratefully received,

thanks. Here is my form:

<?php if (@$_REQUEST['submit']) {
$errorsAndAlerts = "";
$successAlerts = "";

// error checking
if (!@$_REQUEST['title']) { $errorsAndAlerts .= "Please enter your name<br/>\n"; }

// turn off strict mysql error checking for: STRICT_ALL_TABLES
mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)

// add record
if (!@$errorsAndAlerts) {
mysql_query("INSERT INTO `{$TABLE_PREFIX}registration_forms` SET
title  = '".mysql_real_escape_string( $_REQUEST['title'] )."',

Monday = '".(@$_REQUEST['Monday'] ? '1' : '0')."',
Tuesday = '".(@$_REQUEST['Tuesday'] ? '1' : '0')."',
Wednesday = '".(@$_REQUEST['Wednesday'] ? '1' : '0')."',
Thursday = '".(@$_REQUEST['Thursday'] ? '1' : '0')."',
Friday = '".(@$_REQUEST['Friday'] ? '1' : '0')."',

createdDate      = NOW(),
updatedDate      = NOW(),
createdByUserNum = '0',
updatedByUserNum = '0'")
or die("MySQL Error Creating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
$recordNum = mysql_insert_id();

// send email alert
$to="xxxx@xxxx.co.uk"; 
$subject="A new registration form has been added to the database"; 
if(@$_REQUEST['info_request']){$_REQUEST['info_request']=1;}else{$_REQUEST['info_request']=0;} 
$message=<<<__TEXT__

Name: {$_REQUEST['title']} 
Monday: {$_REQUEST['Monday']}
Tuesday: {$_REQUEST['Tuesday']}
Wednesday: {$_REQUEST['Wednesday']}
Thursday: {$_REQUEST['Thursday']}
Friday: {$_REQUEST['Friday']}


__TEXT__;
$from="FROM: xxxx@xxxx.co.uk";
mail($to,$subject,$message,$from);

// display thanks message and clear form
$successAlerts = "Thanks, we have received your registration form.";
$_REQUEST = array();
}
}
?>

<?php if (@$errorsAndAlerts): ?>
<div class="alert alert-danger">
<p><?php echo $errorsAndAlerts; ?></p>
</div>
<?php endif ?>

<?php if (@$successAlerts): ?>
<div class="alert alert-success">
<p><?php echo $successAlerts; ?></p>
</div>
<?php endif ?>

<fieldset>

<form method="post" action="">
<input type="hidden" name="submit" value="1" />


<div class="form-group">
<label class="control-label" for="title"><h3>Your Details</h3></label>  
  
 
<div class="form-group">
<label class="control-label" for="title">Name</label>  
<div>
<input id="textinput" name="title" type="text" placeholder="Name" class="form-control input-md" value="<?php echo htmlspecialchars(@$_REQUEST['title']) ?>">
</div>
</div>



<div class="form-group">
<label class="control-label" for="title"><h3>Booking Details</h3></label>  

<div class="form-group">
<label class="control-label" for="title">Day(s) you would like your dog walked</label>  
<div class="checkbox">
<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox1" name="Monday" > Monday
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox2" name="Tuesday"> Tuesday
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox3" name="Wednesday"> Wednesday
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox4" name="Thursday"> Thursday
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox4" name="Friday"> Friday
</label>
</div>
</div>

</div>

<div class="control-group marginBottom">
<div class="controls">
<button id="singlebutton" type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
</fieldset>

</div>

</div>

<?php include('common/inc/footer.php') ?>
</body>
</html>

Jeff

By Daryl - October 6, 2015 - edited: November 27, 2015

Hi Jeffncou,

As the error says in your example, the variable  $_REQUEST['Monday'] is not defined.
One way to resolve this is to add a hidden field in the form before the <input type="checkbox" value="0"> so that the form will be submitted with $_REQUEST['Monday'] even though the user didn't "checked" the checkbox.
For example:

<label class="checkbox-inline">
  <input type="hidden" name="Monday" value="0" >
  <input type="checkbox" id="inlineCheckbox1" name="Monday" > Monday
</label>

Or, you can use a shorthand IF statement and display a more readable value instead of "1" and "[blank]" in the email.
For example:

// send email alert
$mon = @$_REQUEST['Monday']    ? "Yes" : "No";
$tue = @$_REQUEST['Tuesday']   ? "Yes" : "No";
$wed = @$_REQUEST['Wednesday'] ? "Yes" : "No";
$thu = @$_REQUEST['Thursday']  ? "Yes" : "No";
$fri = @$_REQUEST['Friday']    ? "Yes" : "No";

$to="xxxx@xxxx.co.uk"; 
$subject="A new registration form has been added to the database"; 
if(@$_REQUEST['info_request']){$_REQUEST['info_request']=1;}else{$_REQUEST['info_request']=0;} 
$message=<<<__TEXT__
Name: {$_REQUEST['title']} 
Monday: {$mon}
Tuesday: {$tue}
Wednesday: {$wed}
Thursday: {$thu}
Friday: {$fri}
__TEXT__;
$from="FROM: xxxx@xxxx.co.uk";
mail($to,$subject,$message,$from);

Cheers,

Daryl Maximo
PHP Programmer - interactivetools.com

By Daryl - November 27, 2015

Hi Jeffncou,

I've corrected my example code.

Thanks for pointing that out.

Cheers,

Daryl Maximo
PHP Programmer - interactivetools.com