intermittent duplicate emails from php contact form - php

First time posting here. I have a php contact form on a CMS site that is sending me duplicate emails. The problem is intermittent. Sometimes it send two copies, sometimes it sends as many as 8 copies. Sometimes it works normally. The issue started after removing some fields from the form.
The form submits to a separate page that sends the email to 3 recipients. This page also serves as a thank you page that displays the information submitted by the user. "Thanks for contacting us the following information has been received".
The embeds are expresionengine code. Thanks for your help!!!
Here is the form:
<form id="Form1" name="Form1" method="post" action="http://myprocessingpage.com">
<label for="fname">Name:</label> <input type="text" size="42" id="fname" name="fname"><br>
<label for="email">Email Address:</label> <input size="42" type="text" id="email" name="email"><br>
<label for="telephone">Telephone:</label> <input size="42" type="text" id="telephone" name="telephone"><br>
<br>
<div style="float: left; margin-right: 20px; margin-bottom: 30px;">
<label>How did you hear about us?</label> <br>
<input type="hidden" id="arrayfix" name="how[]" value="">
<input type="checkbox" id="website" name="how[]" value="website"> Website<br>
<input type="checkbox" id="referral" name="how[]" value="referral"> Referral<br>
<input type="checkbox" id="tradeshow" name="how[]" value="tradeshow"> Tradeshow<br>
</div>
<div style="float: left; margin-bottom: 30px;">
<label >Shelter Type:</label><br>
<input type="hidden" id="arrayfix2" name="type[]" value="">
<input type="checkbox" id="safe4x6" name="type[]" value="Safe_Room_4X6"> 4'X6' Shelter<br>
<input type="checkbox" id="safe4x8" name="type[]" value="Safe_Room_4X8"> 4'X8' Shelter<br>
<input type="checkbox" id="custom" name="type[]" value="Custom_Size_Safe_Room"> Custom Size Shelter<br>
</div>
<div style="clear: both;"></div>
<label for="question"> Questions or Comments:</label><br> <textarea rows="7" maxlength="500" name="question" id="question" cols="50"></textarea><br>
<br>
<input type="submit" class="btnimage" id="submit" name="submit" value="">
</form>
Here is the processing page (http://myprocessingpage.com):
<?php
if (isset($_POST['fname']))
{
?>
<!DOCTYPE html>
<head>
<title>Thank you for contacting us!</title>
{embed="page/headtags"}
</head>
<body>
{embed="page/header"}
{embed="page/drop"}
<div id="contentbg">
<div id="content">
<?php
$name = $_POST['fname'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$question = $_POST['question'];
$howarray = $_POST['how'];
$howimplode = implode("\n", $howarray);
$how = str_replace("_", " ", "$howimplode");
$typearray = $_POST['type'];
$typeimplode = implode("\n", $typearray);
$type = str_replace("_", " ", "$typeimplode");
//sent to us
$to = "mail1#mail.com, mail2#mail.com, mail3#mail.com";
$subject = "Info Request";
$message = "INFO REQUEST:\n\nName: $name\n\nEmail: $email\n\nTelephone: $telephone\n\nHow they heard about us:\n$how\n\nShelter type:\n$type\n\nQuestions or Comments:\n$question";
$from = "info#mysite.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
?>
<div id="form">
<p style="margin-top: 0px; margin-bottom: 40px; color: green; font-weight: bold;">Thank you for contacting us! The following information has been received:</p>
<div style="margin-left: 30px;">
<?php
echo "<p><b>Name:</b> ".$name."</p>";
echo "<p><b>Email:</b> ".$email."</p>";
echo "<p><b>Telephone:</b> ".$telephone."</p>";
$thankshowimplode = implode("</br>", $howarray);
$thankshow = str_replace("_", " ", "$thankshowimplode");
$thankstypeimplode = implode("</br>", $typearray);
$thankstype = str_replace("_", " ", "$thankstypeimplode");
echo "<p><b>How you heard about us:</b></br> ".$thankshow."</p>";
echo "<p><b>Type of shelter(s):</b></br> ".$thankstype."</p>";
echo "<p style='word-wrap:break-word;'><b>Questions or Comments:</b> ".$question."</p>";
?>
</div>
</div>
</div>
{embed="page/footer"}
</body>
</html>
<?php
}
else
{
?>
<script type="text/javascript">
window.location = 'http://mycontactpage.com';
</script>
<?php
}
?>

If someone double-clicks (or clicks it 8 times) your submit button, your script could be run more than once. Try disabling the submit button on click (using Javascript).
Another, probably unnecessary, solution would be to lock your form script (e.g. using semaphores), create a database entry with the provided info, then check to see if you've sent the same info recently (e.g. within the last five minutes), and don't sent it again if you have.
I'm sure the Javscript disable functionality will suffice :)
Or, if you want to find out what's really going on, you could just create a log file that logs "sent [some distinct piece of data that might tell these posts apart] at [timestamp]". If you see a couple of these, with the same [distinct info] entry and really close timestamp, then your problem is multiple clicks to the submit button.

Related

Cannot send form with checkboxes PHP

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 ?

How to use PHP to make a form send an email?

I've been learning PHP for about two days now. So I'm really new to PHP. I have tried my best to understand but no matter what I write, my code never works. I got it to send an email successfully one time. But I'm not able to get it to send the contents in my form.
Here is my HTML form.
<!-- FORM BEGINS -->
<form action="contact-form-handler.php" method="post">
<!-- Full Name -->
<div class="formWrapper ">
<label name="firstname" for="firstName"><span>*</span>First Name</label>
<input type="text" name="memberFirstName" id="memberFirstName" onkeyup="activateButton()" class="inputText" tabindex="1" maxlength="256" onkeydown="return checkAlphaNumericText(event,this);" required="required"/>
</div>
<p class="fieldsetDivider"></p>
<div class="formWrapper">
<label for="lastName"><span>*</span>Last Name</label>
<input type="text" name="memberLastName" id="memberLastName" onkeyup="activateButton()" class="inputText" tabindex="2" maxlength="256" onkeydown="return checkAlphaNumericText(event,this);" required="required"/>
</div>
<p class="fieldsetDivider"></p>
<!-- Date of Birth-->
<p class="fieldsetDivider"></p>
<div class="formWrapper smallInput">
<label for="firstInput"><span>*</span>Date of Birth
<div class="labelClarify">(MM/DD/YYYY)</div>
</label>
<div class="inlineInput">
<input type="text" name="month" id="month" maxlength="2" onkeyup="activateButton();autotab(this, 'day')" onkeypress="return isNumberKey(event)" class="inputText" placeholder="MM" required="required"/>
<span>/</span>
</div>
<div class="inlineInput">
<input type="text" name="day" id="day" maxlength="2" onkeyup="activateButton();autotab(this, 'year')" onkeypress="return isNumberKey(event)" class="inputText" placeholder="DD" required="required"/>
<span>/</span>
</div>
<div class="inlineInput">
<input type="text" name="year" id="year" maxlength="4" onkeyup="activateButton()" onkeypress="return isNumberKey(event)" class="inputText" placeholder="YYYY" required="required"/>
</div>
<a rel="Please enter your date of birth" class="formHelp tooltip-link" href="#">
<img src="../../images/site3/common/form_icon_help.png">
</a>
</div>
<p class="fieldsetDivider"></p>
<!-- Member ID -->
<div class="formWrapper">
<label for="memberID"><span>*</span>Member ID</label>
<div class="inlineInput">
<input type="text" name="memberID" id="memberID" class="inputText" onkeyup="activateButton()" tabindex="3" maxlength="9" onkeypress="return isNumberKey(event)" required="required"/>
</div>
<a rel="Please enter your member ID" class="formHelp tooltip-link" href="#">
<img src="../../images/site3/common/form_icon_help.png">
</a>
</div>
<p class="fieldsetDivider"></p>
<!-- Zip Code -->
<div class="formWrapper">
<label for="zipCode"><span class="requiredStar">*</span>Zip Code</label>
<div class="inlineInput">
<input type="text" name="memberZipCode" id="memberZipCode" onkeyup="activateButton()" tabindex="4" class="inputText" maxlength="5" onkeypress="return isNumberKey(event)"/>
</div>
</div>
<p class="fieldsetDivider"></p>
<!-- Button -->
<div class="button">
<button class="greenBtnLarge" type="submit" style="text-decoration:none;"><span>Next<span></button>
</div>
</form>
<!-- FORM ENDS -->
Can someone please help me with a php code that would work for this form? I have found a couple template forms on the internet and I've spent hours trying to modify them to work with my form. But no luck.
And I can get the PHP and form to work together and make it send me an email from my server, so there's no problem with the server. An email will send. I just can't get the PHP code to send the contents of the form. It sends the email, but with no form details. Understand?
I really, really need help with this. Can you guys help me create a PHP code that will work with my HTML form I posted in this thread? I really need to get past this step in this project.
Thank you!
UPDATE!
Here is PHP template I found on the internet, but I don't know how to really modify it properly.... please help?
<?php
$errors = '';
$myemail = 'example#example.com';//<-----Put Your email address here.
if(empty($_POST['firstname']) ||
empty($_POST['lastname']) ||
empty($_POST['dob']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['firstname'];
$email_address = $_POST['lastname'];
$message = $_POST['dob'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact form handler</title>
</head>
<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
</body>
</html>
You have a several code errors...
Remove the <label> tags from your titles for <input type="text">... they are not needed.
Make sure your form field name matches your email code $_POST['something'] for example
You have <input type="text" name="memberFirstName" id="memberFirstName" /> in your form... BUT
you have $_POST['firstname'] in your email code. Change $_POST to memberFirstName OR change your <input name and id to firstname
The same changes need to be made to all elements so they match precisely.
AND remember... PHP is case sensitive.
The HTML form fields are not the same with the fields in php email handler
Ex.
The input name should be same as what the handler should get
Since
<input type="text" name="memberFirstName" required="required"/>
The index of $_POST should be same memberFirstName
$_POST['memberFirstName']
This should be applied to all fields
It seems the HTML form is not related to the php handler at all.
Its better to use this form for testing instead
<form action="contact-form-handler.php" method="POST">
First Name: <input name="firstname" type="text"><br>
Last Name:<input name="lastname" type="text"><br>
DOB: <input name="dob" type="text">
<button type="submit">Send</button>
</form>
Note:
If you are using wamp or xampp local server you need to check if the email service is configured correctly otherwise no email will be sent
Check this for reference:
How to configure XAMPP to send mail from localhost?

php contact form sending blank emails sometimes

I've gone thru tons of the forms and cant seem to find the answer. I have been working on this problem with my php form on and off for days. hope to find help here. the form is working perfect. all the fields are working correct upon submit, but there always seems to be a second form sent out from a day to two days later that is blank. If there is any suggestions to why this occurs it would be helpful.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
// recipient address
$to = "grandforkssuites#gmail.com";
// subject of email
$re = "Contact Us Form Delivery";
// message creation
$contact = "Name:".$name."\nEmail:".$email."\nSubject:".$subject."\r\n";
$txt = "Comments:".$comments."\r\n";
$fmsg = $contact."\r\n".$txt;
$msg = wordwrap($fmsg, 70);
// send email
mail($to,$re,$msg);
?>
<form action="contact1.php" method=post name="form" id="form">
<div class="col_w280 float_l">
<p><em>
<label for="author">Name:</label> <input type="text" id="name" name="name" class="required input_field" />
<div class="cleaner_h10"></div>
<label for="email">Email:</label> <input type="text" id="email" name="email" class="validate-email required input_field" />
<div class="cleaner_h10"></div>
<label for="email">Phone:</label> <input type="text" id="phone" name="phone" class="required input_field" />
<div class="cleaner_h10"></div>
<label for="subject">Subject:</label> <input type="text" name="subject" id="subject" class="input_field" />
<div class="cleaner_h10"></div>
</div>
<div class="col_w280 float_r">
<label for="text">Comments:</label> <textarea id="comments" name="comments" rows="0" cols="0" class="required input_field"></textarea>
<div class="cleaner_h10"></div></em></p>
<input name=submit type=submit id="submit" onClick="MM_validateForm('name','','R','email','','RisEmail');return document.MM_returnValue" value="Send">
</div></form>
Add validation to the PHP, else even if no values was sent via POST, just by visiting the page its going to send a blank email. Most likely a search engine or such bot is just crawling.
So check its POST
<?php
if($_SERVER['REQUEST_METHOD']==='POST'){
//put code here
}
?>
and check your values are set min-max length ect
<?php
...
...
...
//Comments
if(empty($_POST['comments'])){
//comments empty, do or set something
}else if(strlen($_POST['comments']) < 5){
//not long enough, do or set something
}else if(strlen($_POST['comments']) > 50){
//too large, do or set something
}
?>
and most importantly check email is really an email..
<?php
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
//is an email
}else{
//not an email
}
?>
Also your want to add a basic captcha else your be enjoying 1000s of marketing/spam emails per day.
Good luck, implementing it.

Wordpress giving me a 404 error for php form

Since the Wordpress form seems to not want to send me the password to my account for help on their forums I figured I would start here.
I have created a form for my website so that someone can contact us.
But when the person clicks the 'send' button it takes them to a 404 page, which the address reads:
website address - /working/mail.php
I know Wordpress supports .php files and I have mine correctly named and wasn't sure if I am missing something
<div id="FGSform">
<form action="mail.php" method="post" name="contactFGS" id="contactFGS">
<ul>
<li>
<label for="first-name">First Name</label>
<br>
<input type="text" id="firstname" name="firstname" required aria-required="true">
</li>
<br>
<li>
<label for="last-name">Last Name</label><br>
<input type="text" id="lastname" name="lastname" required aria-required="true">
</li>
<br>
<li>
<label for="email">Email</label>
<br>
<input type="email" id="email" name="email" required aria-required="true">
</li>
<br>
<li>
<label for="contact-reason" id="reason" name="reason">Reason for Contact</label>
<select required id="reason" name="reason">
<option value="3">Employment</option>
<option value="1">Print Services</option>
<option value="2">Design Services</option>
<option value="4">Questions</option>
<option value="5">Other</option>
</select>
</li>
<br>
<li>
<label for="comments">Comments</label>
<br>
<textarea name="comments" id="comments" cols="40" rows="10" required></textarea>
</li>
<br>
<li>
<input type="radio" id="newsletter" name="newsletter">
<label for="signmeup">Sign me up for newsletter, updates and other information about FGS</label>
</li>
<br>
<li>
<input type="submit" value="Send">
</li>
Here is my .php file:
<?php
/* Email Variables */
$emailSubject = 'mail!';
$webMaster = 'kmurray.1#frgraphicsolutions.com';
/* Data Variables */
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$email = $_POST['email'];
$body = <<<EOD
<br><hr><br>
Name: $name <br>
Last Name: $lastname <br>
Email: $email <br>
Comments: $comments <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body,
$headers);
/* Results rendered as HTML */
$theResults = <<<EOD
<html>
<head>
<title>sent message</title>
<meta http-equiv="refresh" content="3;URL=http://frgraphicsolutions.com/working/?page_id=8">
<style type="text/css">
<!--
body {
background-color: #444;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 20px;
font-style: normal;
line-height: normal;
font-weight: normal;
color: #fec001;
text-decoration: none;
padding-top: 200px;
margin-left: 150px;
width: 800px;
}
-->
</style>
</head>
<div align="center">Thank you!</div>
</div>
</body>
</html>
EOD;
echo "$theResults";
?>
I have never created a form before, but Wordpress plug-ins are terrible designed and/or over complicate the form giving me little control over what I want in it.
Any help is much appreciated.
I cannot post comments yet or I would add this as a comment but it seems there is no mail.php file. Can you check the full path to the file? It's not in / or in /working/.
Edit in response to your comment:
You should try two things in that order:
Output the result of the form just to check that you are reaching
the form processing file and that everything is working on that
side.
Send an email to yourself using basic parameters, in a file with no
link with the form. This way you'll know if you are able to send
emails properly and you'll be closer to a solution.
Let us know what you find.
After time spent with the OP to find out what the problem was, have determined the following solution:
Set your your form action to this
<form action="/working/wp-content/themes/NEW/mail.php" method="post" name="contactFGS" id="contactFGS">
Since the mail.php file (handler) was inside a themes folder, and not as previously thought.
Change the path in the form tag to /mail.php or whatever is the url of mail.php
EDIT:
Incase it's not clear, just change the form action to the url of your mail.php..

Easiest way to submit data via PHP?

I'm new to PHP, and have spent 10 hours trying to figure this problem out.
The goal is to take all data entered into this order form, and send it to my email via PHP.
I have 2 questions:
1. I can get PHP to send data from a single menu item (example: Mexican Tortas), but how do I get PHP to send data from multiple items (example: Mexican Tortas, Fish Sandwich and Hamburger)?
2. How do I tell PHP to not send data from menu items that don't have the "How Many?" or "Customize It?" text fields filled out?
If you could provide a super simple example (or a link to a learning resource) I would really appreciate it.
Thank you,
Abijah
PHP
<?php
if(isset($_POST['submit'])) {
$to = "test#mywebsite.com";
$subject = "New Order";
$name_field = $_POST['name'];
$phone_field = $_POST['phone'];
$item = $_POST['item'];
$quantity = $_POST['quantity'];
$customize = $_POST['customize'];
}
$body = "Name: $name_field\nPhone: $phone_field\n\nItem: $item\nQuantity: $quantity\nCustomize: $customize";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
?>
HTML
<form action="neworder.php" method="POST">
<div class ="item">
<img style="float:left; margin-right:15px; border:1px Solid #000; width:200px; height:155px;" src="images/mexicantortas.jpg">
<h1>Mexican Torta - $8.50</h1>
<input name="item" type="hidden" value="Mexican Torta"/>
<h2>How Many? <font color="#999999">Ex: 1, 2, 3...?</font></h2>
<input name="quantity" type="text"/>
<h3>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></h3>
<textarea name="customize"/></textarea>
</div><!-- ITEM_LEFT -->
<div class ="item">
<img style="float:left; margin-right:15px; border:1px Solid #000; width:200px; height:155px;" src="images/fishsandwich.jpg">
<h1>Fish Sandwich - $8.50</h1>
<input name="item" type="hidden" value="Fish Sandwich"/>
<h2>How Many? <font color="#999999">Ex: 1, 2, 3...?</font></h2>
<input name="quantity" type="text"/>
<h3>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></h3>
<textarea name="customize"/></textarea>
</div><!-- ITEM_LEFT -->
<div class ="item">
<img style="float:left; margin-right:15px; border:1px Solid #000; width:200px; height:155px;" src="images/hamburgers.jpg">
<h1>Hamburger w/ Fries - $7.00</h1>
<input name="item" type="hidden" value="Fish Sandwich"/>
<h2>How Many? <font color="#999999">Ex: 1, 2, 3...?</font></h2>
<input name="quantity" type="text"/>
<h3>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></h3>
<textarea name="customize"/></textarea>
</div><!-- ITEM_LEFT -->
<div class="horizontal_form">
<div class="form">
<h2>Place Your Order Now: <font size="3"><font color="#037B41">Fill in the form below, and we'll call you when your food is ready to be picked up...</font></font></h2>
<p class="name">
<input type="text" name="name" id="name" style="text-align:center;" onClick="this.value='';" value="Enter your name"/>
</p>
<p class="phone">
<input type="text" name="phone" id="phone" style="text-align:center;" onClick="this.value='';" value="Enter your phone #"/>
</p>
<p class="submit">
<input type="submit" value="Place Order" name="submit"/>
</p>
</div><!-- FORM -->
</div><!-- HORIZONTAL_FORM -->
</form>
I suggest you use '[]', as it groups multiple input fields to an array.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);
$to = "test#mywebsite.com";
$subject = "New Order";
$order = array();
$order['name'] = $_POST['name'];
$order['phone'] = $_POST['phone'];
$food = $_POST['food'];
foreach ($food as $type => $value)
if (strlen($value['quantity']) > 0) // assuming 'customize' is optional
$order[$type] = $value;
print_r($order);
}
?>
<html>
<head>
<title>Order now!</title>
<style>label,input,textarea {display:block}</style>
<body>
<?php // You need enctype for '[]' support' ?>
<form action="" method="post" enctype="multipart/form-data">
<div class ="item">
<label>Mexican Torta - $8.50</label>
<b>How Many?</b>
<input name="food[mexican_torta][quantity]" type="text">
<b>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></b>
<textarea name="food[mexican_torta][customize]"></textarea>
</div>
<div class ="item">
<label>Fish - $3.50</label>
<b>How Many?</b>
<input name="food[fish][quantity]" type="text">
<b>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></b>
<textarea name="food[fish][customize]"></textarea>
</div>
<h2>Place Your Order Now:</h2>
<em>Fill in the form below, and we'll call you when your food is ready to be picked up.</em>
<label>Enter your name</label>
<input type="text" name="name">
<label>Enter your phone nr.</label>
<input type="text" name="phone">
<button>Submit</button>
</form>
</body>
User submits (for example I left the fish blank) and you get
Array (
[name] => Fab
[phone] => 1212
[mexican_torta] => Array ( [quantity] => 2 [customize] => Test )
)
Play around a bit with the '[]' to get the array output you really desire. The print_r will show you exactly what you get. From here it is really easy to put the food details in the e-mail
You'll need unique form field names. In other words, you can't have repeated items of the form:
<input name="quantity" type="text"/>
Instead, you'll need unique names, such as:
<input name="quantity_fish" type="text"/>
You can then use PHP to parse $_POST and throw out empty fields if you'd like.

Categories