Sending Checkbox info from php form to email

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

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 JeffC - November 27, 2015 - edited: November 27, 2015

Hi Daryl

Thanks for your help - sorry for the delayed reply! 

Just a quick update for anyone else following Daryl's advice. I just needed to make one small adjustment to Daryl's code, (just a typo where the text was copy and pasted, but I thought it worth posting just incase it is not immediately obvious)

$mon = @$_REQUEST['Monday'] ? "Yes" : "No";
$tue = @$_REQUEST['Monday'] ? "Yes" : "No";
$wed = @$_REQUEST['Monday'] ? "Yes" : "No";
$thu = @$_REQUEST['Monday'] ? "Yes" : "No";
$fri = @$_REQUEST['Monday'] ? "Yes" : "No";

should read

$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";

Jeff

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