I want to send a form that have checkboxes in it, it was working fine but I do not what I did that now I received an error everytime I send it. I think the problem has to do with the checkboxes because I recall that I had problems with them in the past. I am working with a single file in PHP.
This is the code for the form:
<div class="contactFrm">
<?php if(!empty($statusMsg)){ ?>
<p class="statusMsg <?php echo !empty($msgClass)?$msgClass:''; ?>"><?php echo $statusMsg; ?></p>
<?php } ?>
<form action="" method="post">
<h6>Contact Information</h6>
<fieldset id="left">
<h4>First Name</h4>
<input type="text" name="name" required>
<h4>Company Name</h4>
<input type="text" name="compname" required>
<h4>Phone Number</h4>
<input type="text" name="phone" required>
<h4>Date of Arrival</h4>
<input type="date" name="ardate" required>
</fieldset>
<fieldset id="right">
<h4>Last Name</h4>
<input type="text" name="lname" required>
<h4>Website URL</h4>
<input type="text" name="website" required>
<h4>Email</h4>
<input type="email" name="email" required>
<h4>Date of Departure</h4>
<input type="date" name="depdate" required>
</fieldset>
<h4>What is your estimated time of arrival?</h4>
<input type="text" name="estimated_time" required>
<h4>How many groups will you be hosting?</h4>
<textarea name="groups" required> </textarea>
<h4>How many participants per group do you need to accommodate?</h4>
<input type="text" name="mparticipants" required>
<h4>What are the date/time scheduled for each group?</h4>
<textarea name="scheduled" required> </textarea>
<h4>What kind of set-up will you like?</h4>
<p>Additional fees may apply for living room/specialty set-ups.</p>
<p><div class="inline-field">
<label>
<input type="checkbox" name="CheckboxGroup1[]" value="living_room" id="CheckboxGroup1_0">
Living Room</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup1[]" value="conference_room" id="CheckboxGroup1_1">
Conference Room</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup1[]" value="other" id="CheckboxGroup1_2">
Other (please specify at the end of the form)</label>
<br>
</div></p>
<h6>Clients</h6>
<h4>How many individuals/clients from your company will be attending?</h4>
<p>These individuals will be stationed in the client room.</p>
<input type="text" name="attending">
<h6>Services</h6>
<h4>Which technology services are you interested in?</h4>
<p>Check all that apply (All of these services are charged separately. Some will involve additional costs from the estimate provided to you.)</p>
<p><div class="inline-field">
<label>
<input type="checkbox" name="CheckboxGroup2[]" value="sd_dvd" id="CheckboxGroup2_0">
Stationary SD DVD Recording</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup2[]" value="hd_video" id="CheckboxGroup2_1">
Stationary HD Video Recording</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup2[]" value="streaming" id="CheckboxGroup2_2">
High Definition Video Streaming (Focus Vision (SD) streaming is also available)</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup2[]" value="smartboard" id="CheckboxGroup2_3">
Smartboard (also functions as TV/DVD/PC)</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup2[]" value="translation" id="CheckboxGroup2_4">
Translation Equipment</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup2[]" value="videographer" id="CheckboxGroup2_5">
Videographer Assisted Recording (SD/HD)</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup2[]" value="other" id="CheckboxGroup2_6">
Other (please specify on notes section)</label>
<br>
</div></p>
<h4>Will you need meal(s) to be provided?</h4>
<p>You will be able to pick from various menus once the booking is confirmed.</p>
<p><div class="inline-field">
<label>
<input type="checkbox" name="CheckboxGroup3[]" value="participants" id="CheckboxGroup3_0">
Yes, for Participants</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup3[]" value="clients" id="CheckboxGroup3_1">
Yes, for Clients</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup3[]" value="breakfast" id="CheckboxGroup3_2">
Breakfast</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup3[]" value="lunch" id="CheckboxGroup3_3">
Lunch</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup3[]" value="dinner" id="CheckboxGroup3_4">
Dinner</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup3[]" value="snacks" id="CheckboxGroup3_5">
Snacks</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup3[]" value="beverages" id="CheckboxGroup3_6">
Beverages</label>
<br>
</div></p>
<h4>Do you need research service(s)?</h4>
<p>Check all that apply.</p>
<p><div class="inline-field">
<label>
<input type="checkbox" name="CheckboxGroup4[]" value="moderator" id="CheckboxGroup4_0">
Moderator(s)</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup4[]" value="recruiting" id="CheckboxGroup4_1">
Recruiting</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup4[]" value="reporting" id="CheckboxGroup4_2">
Reporting and Analysis</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup4[]" value="quantitative" id="CheckboxGroup4_3">
Quantitative</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup4[]" value="other" id="CheckboxGroup4_4">
Other Research Services (please specify in the notes section)</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup4[]" value="na" id="CheckboxGroup4_5">
N/A</label>
<br>
</div></p>
<h6>Additional Information</h6>
<h4>Please include any additional information or document that would help us facilitate your request.</h4>
<textarea name="additionalinfo"> </textarea>
<h4>Document Upload</h4>
<input type="file">
<input type="submit" name="submit" value="Submit">
<div class="clear"> </div>
</form>
and this is the PHP:
<?php
$statusMsg = '';
$msgClass = '';
if(isset($_POST['submit'])){
// Get the submitted form data
$name = $_POST['name'];
$lname = $_POST['lname'];
$compname = $_POST['compname'];
$phone = $_POST['phone'];
$ardate = $_POST['ardate'];
$website = $_POST['website'];
$email = $_POST['email'];
$depdate = $_POST['depdate'];
$estimated_time = $_POST['estimated_time'];
$groups = $_POST['groups'];
$mparticipants = $_POST['mparticipants'];
$scheduled = $_POST['scheduled'];
$additionalinfo = $_POST['additionalinfo'];
$attending = $_POST['attending'];
$CheckboxGroup1 = (implode("," , $_POST['CheckboxGroup1']));
$CheckboxGroup2 = (implode("," , $_POST['CheckboxGroup2']));
$CheckboxGroup3 = (implode("," , $_POST['CheckboxGroup3']));
$CheckboxGroup4 = (implode("," , $_POST['CheckboxGroup4']));
// Check whether submitted data is not empty
if(!empty($name) && !empty($lname) && !empty($compname) && !empty($phone) && !empty($ardate) && !empty($website) && !empty($email) && !empty($depdate) && !empty($estimated_time) && !empty($groups) && !empty($mparticipants) && !empty($scheduled) && isset($CheckboxGroup1) && isset($CheckboxGroup2) && isset($CheckboxGroup3) && isset($CheckboxGroup4) && !empty($attending) && !empty($additionalinfo)){
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
$statusMsg = 'Please enter your valid email.';
$msgClass = 'errordiv';
}else{
// Recipient email
$toEmail = 'osek2112#gmail.com';
$emailSubject = 'Facility Request Submitted by '.$name;
$htmlContent = '<h2>Facility Request Form</h2>
<h4>Name</h4><p>'.$name.'</p>
<h4>Last Name</h4><p>'.$lname.'</p>
<h4>Company Name</h4><p>'.$compname.'</p>
<h4>Phone Number</h4><p>'.$phone.'</p>
<h4>Date of Arrival</h4><p>'.$ardate.'</p>
<h4>Website URL</h4><p>'.$website.'</p>
<h4>Email</h4><p>'.$email.'</p>
<h4>Date of Departure</h4><p>'.$depdate.'</p>
<h4>What is your estimated time of arrival?</h4><p>'.$estimated_time.'</p>
<h4>How many groups will you be hosting?</h4><p>'.$groups.'</p>
<h4>How many participants per group do you need to accommodate?</h4><p>'.$mparticipants.'</p>
<h4>What are the date/time scheduled for each group?</h4><p>'.$scheduled.'</p>
<h4>What kind of set-up will you like?</h4><p>'.$CheckboxGroup1.'</p>
<h4>Which technology services are you interested in?</h4><p>'.$CheckboxGroup2.'</p>
<h4>How many individuals/clients from your company will be attending?</h4><p>'.$attending.'</p>
<h4>Will you need meal(s) to be provided?</h4><p>'.$CheckboxGroup3.'</p>
<h4>Do you need research service(s)?</h4><p>'.$CheckboxGroup4.'</p>
<h4>Additional Information</h4><p>'.$additionalinfo.'</p>';
// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: '.$name.'<'.$email.'>'. "\r\n";
// Send email
if(mail($toEmail,$emailSubject,$htmlContent,$headers)){
$statusMsg = 'Your contact request has been submitted successfully !';
$msgClass = 'succdiv';
}else{
$statusMsg = 'Your contact request submission failed, please try again.';
$msgClass = 'errordiv';
}
}
}else{
$statusMsg = 'Please fill all the fields.';
$msgClass = 'errordiv';
}
}
?>
Any help pointing out my error would be appreciated.
If you are getting this message:
Your contact request submission failed, please try again.
Then this portion of the code is throwing that message:
// Send email
if(mail($toEmail,$emailSubject,$htmlContent,$headers)){
$statusMsg = 'Your contact request has been submitted successfully !';
$msgClass = 'succdiv';
}else{
$statusMsg = 'Your contact request submission failed, please try again.';
$msgClass = 'errordiv';
}
Which means that the mail() function is returning false. Make sure that the mail() function works by passing in parameters you know are valid, such as those from the other contact form you say is working. Also, maybe try removing the $headers and see if that helps, since that part is optional.
i have checked your code and every time it's working but you don't handle it if checked box not selected that time occurring error just handle it,
may be use miss it checked boxed selection .
other wise what type of error occurring ?
Related
I have a simple html form what I use in all of my websites in different servers.
In a new server I found a problem I cant solve by my own.
The problem is that after hit the submit button php code doesnt send the email to the server email and doesnt direct to the thankyou.html page. But it does direct to mydomain.com/contactform.php and shows the php code. Firstly I though something wrong with the server configuration, but i didnt find anything. Could anyone give my any advice where to search? Thanks in advance!
PHP code:
<?php
if($_SERVER["REQUEST_METHOD"] === "POST") {}
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$place = $_POST['place'];
$number = $_POST['number'];
$csoportjelleg = $_POST['csoportjelleg'];
$message = $_POST['message'];
$recaptcha_secret = "secret-key";
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$_POST['g-recaptcha-response']);
$response = json_decode($response, true);
if($response["success"] === true){
$mailTo = "info#serveremail.com";
$headers = "From: ".$mailFrom;
$txt = "Feladó: ".$name."\nInnenjönnek: ".$place."\nLétszám: ".$number."\nCsoportjelleg: ".$csoportjelleg."\n\nÜzenet: ".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: thankyou");
}else{
header("Location: error");
}
}
?>
HTML code:
<form action="contactform.php" method="post" class="ajanlatkeres-form">
<h2 class="centered-h2">Általános adatok</h2>
<input type="text" name="name" placeholder="Teljes név*" class="feedback-input" required>
<input type="text" name="mail" placeholder="E-mail*" class="feedback-input" required>
<h2 class="centered-h2">További adatok</h2>
<input type="text" name="place" placeholder="Honnan jöttök?" class="feedback-input">
<input type="text" name="number" placeholder="Hány fős a csoport?" class="feedback-input">
<div class="feedback-input-div">
<p class="form-p">Csoport jelleg?</p>
<label class="container">Munkahelyi
<input type="radio" name="csoportjelleg" required>
<span class="checkmark"></span>
</label>
<label class="container">Iskolai
<input type="radio" name="csoportjelleg">
<span class="checkmark"></span>
</label>
<label class="container">Családi
<input type="radio" name="csoportjelleg">
<span class="checkmark"></span>
</label>
<label class="container">Egyéb
<input type="radio" name="csoportjelleg">
<span class="checkmark"></span>
</label>
</div>
<h2 class="centered-h2">Üzenet</h2>
<input type="hidden" name="subject" value="Csoportos ajánlatkérés">
<textarea name="message" placeholder="Ajánlatkérés üzenete:*" class="feedback-input" required></textarea>
<div class="g-recaptcha" id="rcaptcha" data-sitekey="6LdBDf4UAAAAAJ50InC0WKfVep4263x3Bmuz9-60"></div><br/>
<div class="button-form">
<button id="submit_form" class="contactbutton" type="submit" name="submit">Küldés</button>
</div>
UI.: PHP form location: I use thankyou and error without .html extension because there are no extensions in my ULR-s thanks to .htaccess manipulations. In other servers that was absolutly ok.
Updated, thanks kerbh0lz!
I have a html form where i want 3 fields to be mandatory. If the user doesn't fill any one of those fields, then the form shouldn't be submitted and it should tell the user to fill in the mandatory one's. I've used PDO and i dont know how to do it. If someone could help me. Down below i've given both my html and php files.
HTML:
<html>
<head>
<title>Data Insertion</title>
</head>
<body>
<p><span class="Error">* required field.</span></p>
<form method="post" action="su.php">
<h2>Please Fill In Details</h2>
<label for="name">Name </label>
<input type="text" Placeholder="Enter your name" name="name" id="name" />
<span class="Error">*</span>
<br />
<br />
<label for="age">Age </label>
<input type="text" name="age" id="age" placeholder="Enter your age" />
<br />
<br />
<label for="mailid">MailId </label>
<input type="text" name="mailid" id="mailid" placeholder="Enter your Mail Id" />
<span class="Error">*</span>
<br />
<br />
<label for="gender">Gender </label>
<br />
<label for="male">Male </label>
<input type="radio" name="gender" id="gender" value="Male" id="male" />
<label for="female">Female </label>
<input type="radio" name="gender" id="gender" value="Female" id="female" />
<br />
<br />
<label for="qualification">Qualification </label>
<select name="qualification" value="Qualification" id="qualification">
<option value="B.E">SSLC</option>
<option value="P.G">HSC</option>
<option value="SSLC">UG</option>
<option value="HSC">PG</option>
</select>
<br /><br />
<label for="hobbies">Hobbies </label>
<br />
<input type="checkbox" name="hobbies" id="hobbies" value="Cricket" />Cricket
<input type="checkbox" name="hobbies" id="hobbies" value="Music" />Music
<input type="checkbox" name="hobbies" id="hobbies" value="Swimming" />Swimming
<br /><br />
<label for="textarea">Address </label>
<br />
<textarea name="address" id="textarea" rows="15" cols="30"></textarea>
<span class="Error">*</span>
<br /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
PHP:
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
try {
$conn = new PDO("mysql:host=$servername;dbname=testing", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(isset($_POST['submit'])){
$name = $_POST['name'];
$age = $_POST['age'];
$mailid = $_POST['mailid'];
$gender = $_POST['qualification'];
$hobbies = $_POST['address'];
if($name !='' || $mailid !='' || $address !=''){
$sql = "Insert into user (Name, Age, MailId, Gender, Qualification, Hobbies, Address)
values ('".$_POST["name"]."', '".$_POST["age"]."', '".$_POST["mailid"]."', '".$_POST["gender"]."', '".$_POST["qualification"]."', '".$_POST["hobbies"]."', '".$_POST["address"]."')";
$conn->exec($sql);
echo "Thank you for registering";
} else {
echo "<p>Insertion failed <br/> Please enter the required fields !";
}}
}
catch(PODException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
?>
Try adding the html 5 attribute "required" on all the required input elements. For example
<input type="text" Placeholder="Enter your name" name="name" id="name" required />
You should also check the POST variables in the php code though, as this doesn't really prevent someone from abusing your service. Ex.
if(!isset($_POST['somevar'])) {
// Do insert
}
In html You should use javascript (e.g. jQuery) to control onsubmit event and validate if mandatory fields are filled with proper values, check this link: jQuery.submit()
In php You should check and validate each var before create and execute query. Simple article about it is here: Sanitize and Validate Data with PHP Filters
This would be good for the start I think ;)
Query string should contain placeholders and then statement should be prepared for execution, check this link:
PDOStatement::bindParam
I am using a custom optin page with a form , While testing and in work,When I submit the form I get all the details on my email except the phone number , I do not see any problem with the codes , I tried diff things like changing the value and stuff but it did not work, Here is the form code
<div class="form fix ">
<p class="form-text">Fill This Out and See Your <br>Timeshare Report</p>
<form name="contactform" action="mail-script.php" method="POST">
<label for="fname">First Name:
<input type="text" name="fname" id="fname" />
</label><br>
<label for="lname">Last Name:
<input type="text" name="lname" id="lname" />
</label><br>
<label for="email">Email Address:
<input type="text" name="email" id="email" />
</label><br>
<label for="phone">Phone Number:
<input type="text" name="phone" id="phone" />
</label><br>
<label for="phone">Alternate Phone:
<input type="text" name="phone" id="aphone" />
</label><br>
<label for="resort">Resort Name:
<input type="text" name="resort" id="resort" />
</label><br>
<label for="amount">Amount Owed? $:
<input type="number" name="amount" id="amount" />
<p style="font-size: 12px !important;margin-top: -14px;padding-right: 30px;text-align:right;">
If Paid Off Leave Zero, Else Put Amount</p>
</label><br>
<div class="checkbox">
<div class="check-text fix">
<p>I'm Considering To</p>
</div>
<div class="check-one fix">
<input type="checkbox" name="call" id="" value="sell"/> Sell It <br>
<input type="checkbox" name="call" id="" value="buy"/> Buy It <br>
<input type="checkbox" name="call" id="" value="rent "/> Rent It
</div>
<div class="check-two fix">
<input type="checkbox" name="call" id="" value="cancel"/> Cancel Mortgage <br>
<input type="checkbox" name="call" id="" value="ownership"/> End Ownership <br>
<input type="checkbox" name="call" id="" value="give"/> Give It Back
</div>
</div>
<p class="captcha">
<img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br>
<label for='message'>Enter the code above here :</label><br>
<input id="6_letters_code" name="6_letters_code" type="text"><br>
<small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small>
</p>
<input id="submit" type="submit" value="" />
<p class="submit-text">Ensure all fields are completed and correct, allowing you more benefits, while preventing abuse of our data.</p>
</form>
</div>
</div>
This is the mail script which sends me the email
<?php
/* Set e-mail recipient */
$myemail = "**MYEMAIL**";
/* Check all form inputs using check_input function */
$fname = check_input($_POST['fname'], "Enter your first name");
$lname = check_input($_POST['lname'], "Enter your last name");
$email = check_input($_POST['email']);
$phone = check_input($_POST['phone']);
$resort = check_input($_POST['resort']);
$amount = check_input($_POST['amount']);
$call = check_input($_POST['call']);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
$website = '';
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
First Name : $fname
Last Name : $lname
E-mail: $email
Phone : $phone
Resort: $resort
Amount: $amount
Call : $call
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: index.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
if (strtolower($_POST['code']) != 'mycode') {die('Wrong access code');}
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
Here is the online url of the page
http://timesharesgroup.com/sell/index.html
You have two form elements with the same name. The second one overwrites the first one and you never receive it. You also forget your alternate phone number in your PHP code.
<label for="phone">Phone Number:
<input type="text" name="phone" id="phone" />
</label><br>
<label for="phone">Alternate Phone:
<input type="text" name="altphone" id="aphone" />
</label><br>
Your PHP:
$phone = check_input($_POST['phone']);
$altphone = check_input($_POST['altphone']);
Phone : $phone
Alt Phone : $altphone
Resort: $resort
Your alternative phone number is overwriting your phone number:
<label for="phone">Phone Number:
<input type="text" name="phone" id="phone" />
</label><br>
<label for="phone">Alternate Phone:
<input type="text" name="phone" id="aphone" />
^^^^^ here
</label><br>
Changing the name for the alternative number should fix that.
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 & 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.
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).
The $email variable is not defined.
Put this:
$email = $_REQUEST['email'];
before the mail function call, and use mail($email, ... for it to work.
I am trying to create a check list that allows users to check off each item they want and then send an email to their chosen email. I am using JQueryMobile and I don't know if it's causing any problems, but the page just keeps on loading continuously when I press submit.
This is my HTML code:
<form name="emailform" enctype="multipart/form-data" action="form-to-email.php" method="post">
<div data-role="fieldcontain">
<label for='name'>Festival name: </label><br>
<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 & Cards
</label>
<input name="checkbox5" id="checkbox5" type="checkbox" />
<label for="checkbox5">
Keys
</label>
</fieldset>
<label for='email'>Send to Email:</label><br>
<input type="text" name="email">
<input name="share" type="submit" value="Share">
</form>
And this is my PHP:
<?php
if(!isset($_POST['submit'])){
if (!$_POST['name'] | !$_POST['email'])
{
echo"<div class='error'>Error<br />You did not fill in a required field, please review your form and correct the missing information.</div>";
}
}
$name = $_POST['name'];
$email = $_POST['email'];
$checkbox1 = $_POST['checkbox1'];
$email_from = "Application";
$email_subject = $name;
$email_body = "You have received a new checklist via App.\n".
"Here is the checklist so far:\n $checkbox1".
$headers = "From: $email_from \r\n";
mail($email, $email_subject,$email_body,$headers);
header('Location: index.html');
?>
If you are trying to send email from localhost, then you will definitely face this error as there are no mail settings by default. However you can install a test mail server on your computer to test emails from localhost.
If you are sending the email from live server then you need to follow this tutorial to overcome any issues that might be in your code: http://www.w3schools.com/php/php_mail.asp
I think you should communicate through AJAX with jQuery, In that case you should NOT send headers. Also you should note that checkbox values are TRUE/FALSE, so
"You have received a new checklist via App.".
"Here is the checklist so far: ".