Nicola Conee
Nicola Conee

Reputation: 167

How to send form check list data with PHP to a specific email

I'm really struggling to find a way to send checklist data to an email, let alone a user input email! I have tried following and applying the answers from other similar questions asked but still to no avail.

This is my PHP so far, although I am quite confused.

<?php 
//Check for POST
if (isset($_REQUEST['email'])){
//All your inputs
$expecting = array('checkbox1','checkbox2','checkbox3','checkbox4' ,'checkbox5');

//Start building your email
$email_content = '';
foreach($expecting as $input){
    //Is checkbox?
    if(substr($input,0,8)=='checkbox'){
        $email_content .= ucfirst($input).':'.(isset($_POST[$input]) && $_POST[$input] == 'on' ? 'True' : 'False'.'<br />');

    }else{
        $email_content .= ucfirst($input).':'.(!empty($_POST[$input]) ? $_POST[$input] : 'Unknown').'<br />';
    }
}
print_r($email_content);


if(mail('$email',  'packing list', wordwrap($email_content))){
   //mail sent
}else{
   //mail failed 2 send
}

}
?>

And this is my HTML

  <form name="emailform"  method="post" action="send-list.php">
  <div data-role="fieldcontain">
  <label for='name'><h3>Festival name:</h3> </label>
  <input type="text" name="name"> 



<h3>
Essentials
</h3>

<fieldset data-role="controlgroup" data-type="vertical">
<legend>
</legend>
<input name="checkbox1" id="checkbox1" type="checkbox" />
<label for="checkbox1">
Tickets
</label>
<input name="checkbox2" id="checkbox2" type="checkbox" />
<label for="checkbox2">
Parking pass
</label>
<input name="checkbox3" id="checkbox3" type="checkbox" />
<label for="checkbox3">
Directions
</label>
<input name="checkbox4" id="checkbox4" type="checkbox" />
<label for="checkbox4">
Cash &amp; Cards
</label>
<input name="checkbox5" id="checkbox5" type="checkbox" />
<label for="checkbox5">
Keys
</label>
</fieldset>

Email: <input name='email' type='text'><br>
<input type="submit" value="Send">

I would be happy if I could just get the checked boxes to send to an email and from there, I would hopefully be able to work out how to send the information to a user input email.

Upvotes: 0

Views: 1087

Answers (2)

Bart Friederichs
Bart Friederichs

Reputation: 33531

The $email variable is not defined.

Put this:

  $email = $_REQUEST['email'];

before the mail function call, and use mail($email, ... for it to work.

Upvotes: 0

Marc B
Marc B

Reputation: 360762

You're doing it mostly right, but your code is checking of the checkboxes have the value "on"... but never set that value in your form:

<input name="checkbox5" id="checkbox5" type="checkbox" value="on" />
                                                       ^^^^^^^^^^

This is also a syntax bug:

if(mail('$email',  'packing list', wordwrap($email_content))){
        ^--    ^--

single-quoted strings do NOT interpolate variables. so you're trying to send an email to an account named $email, not whatever address is in the $email variable.

Try

if(mail($email,  'packing list', wordwrap($email_content))){

instead (note lack of quotes).

Upvotes: 2

Related Questions