Form waits for second submission to push through first submission - php

I'm new to development and I'm trying to build a contact form using php that pulls the info from a contact form. My contact form works and collects the data as needed, but when I click submit the first time I don't receive the information to my inbox. When I refresh the page, complete the form a second time and click submit once more, I receive both submissions to my inbox at the same time. It's as if the first submission waits to be pushed through by the second submission. Can anyone explain why this is happening and how to stop it?
I've seen a lot of posts regarding submissions happening twice but my issue is the first doesn't come through until the second is sent.
Here is my PHP
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$body = $_POST['body'];
$radio = $_POST['time'];
$mailTo = "myemail#gmail.com";
$headers = "From: " . $mailFrom;
$txt = "You have received an enquiry from: " . $name .
"\n\n Phone Number: " . $phone .
"\n\n Address: " . $address .
"\n\n Message: " . $body .
"\n\n Time to call: " . $radio;
mail($mailTo, $subject, $txt, $headers);
header("Location: index-contact.php?submission");
}
I'm testing on localhost8888 using MAMP Pro.
Here is HTML:
<form class="contact-form" action="contactform.php" method="post" id="form">
<label for="name">Full Name:</label><br>
<input type="text" id="name" name="name" placeholder="Please enter your name" required>
<label for="email">Email Address:</label><br>
<input type="email" id="email" name="email" placeholder="Please enter your email address" required>
<label for="phone">Phone Number:</label><br>
<input type="text" id="phone" name="phone" placeholder="Please enter your phone number" required></input>
<label for="subject">Subject</label><br>
<input type="text" id="subject" name="subject" placeholder="Please enter a subject" required></input>
<label for="address">Postcode:</label><br>
<input type="text" name="address" placeholder="Please enter your postcode"></input><br>
<label for="body">Comments:</label><br>
<textarea name="body" placeholder="Please enter your message"></textarea>
<input type="radio" id="morning" name="time" value="morning">Morning</input>
<input type="radio" id="afternoon" name="time" value="afternoon">Afternoon</input>
<input type="radio" id="anytime" name="time" value="anytime">Anytime</input>
<button type="submit" id="submit" name="submit">Send Message</button>
</form>

This happens because when you update the page, you will be resending the POST.
As this is a contact form, i suggest installing Google reCaptcha. Thus, it will be necessary to resolve the captcha and this will not happen.
There are other ways. One of them would be to store user data in a database and only authorize a sending by IP or session at a certain time.
Or you can just work with direct session and prevent this from happening, using the function in your code:
function prevent_multi_submit($type = "post", $excl = "contact") {
$string = "";
foreach ($_POST as $key => $val) {
// this test is to exclude a single variable, f.e. a captcha value
if ($key != $excl) {
$string .= $val;
}
}
if (isset($_SESSION['last'])) {
if ($_SESSION['last'] === md5($string)) {
return false;
} else {
$_SESSION['last'] = md5($string);
return true;
}
} else {
$_SESSION['last'] = md5($string);
return true;
}
}
Your code will look like this:
session_start();
if (isset($_POST['submit']) and prevent_multi_submit()) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$body = $_POST['body'];
$radio = $_POST['time'];
$mailTo = "myemail#gmail.com";
$headers = "From: " . $mailFrom;
$txt = "You have received an enquiry from: " . $name .
"\n\n Phone Number: " . $phone .
"\n\n Address: " . $address .
"\n\n Message: " . $body .
"\n\n Time to call: " . $radio;
mail($mailTo, $subject, $txt, $headers);
header("Location: index-contact.php?submission");
}
EDIT 1:
Or, add a simple parameter to the URL that redirects, so that a second POST is not authorized:
if (isset($_POST['submit']) and $_GET["sent"] != "ok") {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$body = $_POST['body'];
$radio = $_POST['time'];
$mailTo = "myemail#gmail.com";
$headers = "From: " . $mailFrom;
$txt = "You have received an enquiry from: " . $name .
"\n\n Phone Number: " . $phone .
"\n\n Address: " . $address .
"\n\n Message: " . $body .
"\n\n Time to call: " . $radio;
mail($mailTo, $subject, $txt, $headers);
header("Location: index-contact.php?sent=ok");
}
Read more about HTTP GET variables at PHP.net

Related

PHP form with image upload

I'm trying to make a simple contact form page with a combination of HTML and PHP with an upload image file section to it. I am 99% sure I got the HTML part correct but I am just lost with the PHP part.
So here is my HTML
<form class="" action="contactform.php" method="post">
<input type="text" name="firstname" placeholder="First Name" required>
<input type="text" name="lastname" placeholder="Last Name" required>
<input type="text" name="mail" placeholder="Your e-mail" required>
<input type="text" name="phone" placeholder="Phone">
<input type="file" id="myFile" name="filename">
<textarea name="message" placeholder="Message" required></textarea>
<button type="submit" name="submit">Send</button>
</form>
And here is my PHP
if (isset($_POST['submit'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "email#email.com";
$headers = "From: ".$mailFrom;
$subject = "You have received an e-mail from your web site";
$message = "First Name: " . $firstname . " Last Name: " . $lastname . "\r\nPhone: " . $phone . "\r\nemail: " . $mailFrom . "\r\nMessage: " . $message;
$retval = mail( $mailTo, $subject, $message, $headers );
if ( $retval == true ){
echo "<h1>Message Sent</h1>
<p>Thank you for contacting us your message has been sent successfully and we will get back to you a soon as possible.</p>";
}
else {
echo "<h1>Message Sent</h1><p>Message could not be sent Mail server did not accept mail.</p>";
}
}
Does anyone know how I can make the upload file part work?
try changing if($retval==true) to if($retval!=false) because I believe it only returns false if it fails and if it is successful it will return a hash value, so I believe making sure it's not true will work.
Also just you might want to take mail( $mailTo, $subject, $message, $headers ); and copy it into its own function, but I'm not sure. Kinda like this:
$retval = mail( $mailTo, $subject, $message, $headers );
mail( $mailTo, $subject, $message, $headers );
I'm not sure though, I hope this works, sorry if it doesn't!

HTML form to PHP mail script not getting $post data

I'm having some trouble with a basic mail PHP script. I can get the email to send, but can't get the variables from the html form.
Here is the code, any ideas?
<form action='sendmail.php' method="POST">
<label for="name">Name</label>
<input type="text" name="name" id="name" />
<label for="email">Email</label>
<input type="email" name="email" id="email" />
<label for="email">Phone Number</label>
<input type="number" name="number" id="number" />
<label for="comments">Message</label>
<textarea id="comments" name="message"></textarea>
<input type="submit">
</form>
And here is the PHP file
<?php
$name = $_POST["name"];
$email_from = $_POST["email"];
$phone = $_POST["number"];
$message = $_POST["message"];
$to = 'sam.weinhandl#gmail.com';
$subject = 'Contact Form Message';
$message = "Name: ". $name . "\r\nPhone: " . $phone . "\r\nMessage: " . $message;
$headers = "From: " . $email_from . "\r\n" .
"Reply-To: " . $email_from . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
You are using POST method to send form data to PHP. Use $_POST to get the values.
$name = $_POST["name"];
$email_from = $_POST["email"];
$phone = $_POST["number"];
$message = $_POST["message"];
Either change method when posting your form
<form action='sendmail.php' method="GET">
Or
change the variables
$name = $_POST["name"];
$email_from = $_POST["email"];
$phone = $_POST["number"];
$message = $_POST["message"];
You can use $_POST or $_REQUEST to fetch values. $_REQUEST will be the best option because it works for both get and post methods.
$name = $_REQUEST["name"];
$email_from = $_REQUEST["email"];
$phone = $_REQUEST["number"];
$message = $_REQUEST["message"];
U should change your method to 'GET'.
Or u should change the variables.
The answers above all work.

PHP email and url validation

So I've made myself a little contact form with php, css, and html. But when I try to add a email validation it still sends the email and doesn't change the style of the input to red (Like I would like it to). Another issue I'm having is the button redirecting to the top of the page (which I do not want it to do). Last I can I make the input keep the text rather than remove it once submitted
HTML:
<div id="contact">
<div class="container">
<form id="contact-form" method="post">
<h1>Contact Form</h1>
<fieldset>
<input placeholder="Your Name" type="text" name="name" required>
</fieldset>
<fieldset>
<input placeholder="Your Email Address" type="email" name="email" id="email-input" required>
</fieldset>
<fieldset>
<input placeholder="Your Phone Number (optional)" type="tel" name="phone" required>
</fieldset>
<fieldset>
<input placeholder="Your Web Site (optional)" type="url" name="site" required>
</fieldset>
<fieldset>
<textarea placeholder="Type your message here...." name="message" required></textarea>
</fieldset>
<fieldset>
<button type="submit" id="contact-submit" name="submit">Submit</button>
</fieldset>
</form>
</div>
</div>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$visitors_site = $_POST['site'];
$message = $_POST['message'];
$email_from = 'mattmowen1#gmail.com';
$email_subject = 'New Contact Submission';
$to = 'mattmowen1#gmail.com';
$headers = "From:" . $email;
$headers = "Contact Submission From: " . $email;
$message1 = "Name: " . $name;
$message2 = "\n\nEmail: " . $email;
$message3 = "\n\nPhone: " . $phone;
$message4 = "\n\nTheir Site: " . $visitors_site;
$message5 = "\n\nMessage: " . $message;
$email_body = $message1 . $message2 . $message3 . $message4 . $message5;
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
mail($to, $email_subject, $email_body,$headers);
} else {
echo "<style>#email-input {color:red}</style";
}
?>
Try this for email validation in php
<?php
if (isset($_POST) && !empty($_POST)) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$visitors_site = $_POST['site'];
$message = $_POST['message'];
$email_from = 'mattmowen1#gmail.com';
$email_subject = 'New Contact Submission';
$to = 'mattmowen1#gmail.com';
$headers = "From:" . $email;
$headers = "Contact Submission From: " . $email;
$message1 = "Name: " . $name;
$message2 = "\n\nEmail: " . $email;
$message3 = "\n\nPhone: " . $phone;
$message4 = "\n\nTheir Site: " . $visitors_site;
$message5 = "\n\nMessage: " . $message;
$email_body = $message1 . $message2 . $message3 . $message4 . $message5;
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
mail($to, $email_subject, $email_body,$headers);
} else {
echo "<style>#email-input {color:red}</style>";
}
}
?>
As per our chat conversation. I am adding jquery ajax function according to your form requirement.
You need to create new file email.php and put your php code into this separate php file
<script>
var url = 'email.php';
$.ajax({
url : url,
type : "POST",
dataType : "JSON",
data : $('#contact-form').serialize(),
success : function(response) {
if (response.error == 0) { // success
$('#contact-form')[0].reset();
alert('Form submitted successfully. We will contact you asap.');
} else { // error
$('#email-input').css('color', 'red');//in case of email error
alert('ERROR MESSAGE');//form is invalid
}
}
})
</script>
To handle JSON request you need to send JSON object in response. So change you php code snippet like this:
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
mail($to, $email_subject, $email_body,$headers);
exit(json_encode(array('error' => 0)));
} else {
exit(json_encode(array('error' => 1)));
}

Issue with simple PHP Contactform

So I have some issues with my PHP Contactform on a HTML site.
Code below is what I have written in HTML. (that's not te problem)
<form action="sendmail.php" method="POST" class="contact-form">
<input type="text" placeholder="Amount" name="amount">
<input type="text" placeholder="Name" required name="name">
<input type="text" placeholder="Email Address" required name="email">
<div class="validation">
<button class="btn" name="submit">Send request</button>
// END HTML code. (BELOW is starts PHP).
$email_to = "domain#website.com";
$amount = $_POST["amount"];
$name = $_POST["name"];
$email = $_POST["email"];
$email_subject = "DOMAINNAME";
$headers = "From: " . $email . "\n";
$headers .= "Reply-To: " . $email . "\n";
$message = 'Name: ' . $name . ', email: ' . $email . ', amount: ' . $amount;
ini_set("sendmail", $email);
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false)
{$sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email);}
{
header("Location:https://website.com");
} else {
echo "There has been an error sending your comments. Please try later.";
}
What do I wrong? I only wanna receive the --> name, amount and mail.
Only when I press submit on my HTML site, it's send me to website.com/sendmail.php (HTTP ERROR 500).
Thank you guys.

How to apply Google reCAPTCHA (2.0) to my current HTML/PHP contact form

I am trying to apply Google reCAPTCHA to my HTML/PHP contact form. The reCAPTCHA appears on the page and works, but I cannot get it to work with the form.
Can somebody please tell me how to add the appropiate PHP code to integrate the recaptcha into my contact form?
<form action="submit.php" method="POST">
<input class="textbox" type="text" name="fullname" placeholder="Your Name">
<input class="textbox" type="email" name="email" placeholder="E-Mail">
<input class="textbox" type="tel" name="telephone" placeholder="Telephone">
<input class="textbox" type="text" name="business" placeholder="Organization">
<textarea class="comments_box" name="visitor_message" placeholder="Message"></textarea>
<div class="g-recaptcha" data-sitekey="6LddnxAUAAAAAAi6jmmUcX8pPMfSELRgpNUAI2Ra"></div>
<br>
<input class="submitBtn" type="submit" value="SUBMIT">
</form>
<?php
if (isset($_POST['fullname']) && isset($_POST['email']) && isset($_POST['telephone']) && isset($_POST['business']) && isset($_POST['visitor_message'])) {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$telephone= $_POST['telephone'];
$business = $_POST['business'];
$visitor_message = $_POST['visitor_message'];
if (!empty($fullname) && !empty($email) && !empty($telephone) && !empty($business) && !empty($visitor_message)) {
$to = 'acramirez38#gmail.com';
$subject = 'Trucker Radio Talk Visitor Message';
$body = "Full Name: " . $fullname ."\n". "E-Mail: " . $email ."\n". "Telephone: " . $telephone ."\n". "Business: " . $business ."\n". "Comments: " . $visitor_message;
$headers = 'From: ' .$email;
if (mail($to, $subject, $body, $headers))
echo 'Thank you for contacting us! Your message has been successfully delivered.';
} else {
echo 'All fields are required! Please return to the previous page and revise.';
}
}
?>

Categories