Stop form refreshing and duplicate emails sent from form - php

First of all I am wondering if there is a way to make it so the page don't refresh on form submission but the boxes etc. are still all cleared.
But my main issue is, once I submit a form it sends an email. BUT.... now every time I refresh the page or go back on the website it sends the email again. I can also click the submit several times. Any help is greatly appreciated.
Code
<?php
if(isset($_POST['SubmitButton'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "reviews#itssimplytech.co.uk";
$subject = "Simply Tech Contact Form";
$headers = "From: $email \r\n".
"Reply-To: $.email \r\n".
'X-Mailer: PHP/' . phpversion();
mail($recipient, $subject, $formcontent, $headers) or die("Error!");
}
?>
<form class="contact-form" action="" method="post">
<input type="text" name="name" placeholder="Name" required>
<input type="email" name="email" placeholder="Email" required>
<textarea rows="5" name="message" placeholder="Say Something..." required></textarea>
<input type="submit" name="SubmitButton" value="Submit">
</form> <!-- /.contact-form -->

Related

powering contact form with php

I am trying to power my contact form with the following HTML and PHP, and when I enter information and press enter, I always get an Error!
HTML
<form action="contact.php" method="POST">
<input type="text" placeholder="your name" name="name">
<input type="email" placeholder="Your Email" name="email">
<textarea placeholder="Your Message" name="message"></textarea>
<button type="submit">submit</button>
</form>
PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent=" From: $name \n Email: $email \n Message: $message";
$recipient = "jainkenul#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
I have uploaded these files on my server, you can check it here
I don't see any error in your code. Probably, your host may have disabled the mail() function

Not receiving email with PHP form

I'm trying to make a simple html form with php to send messages to a specific email. A friend sent me this form but since i'm very noobish in PHP i can't seem to figure out why i'm not receiving any emails...
This is my HTML CODE:
<div class="form">
<form id="contactform" action="mailer.php" method="post">
<p class="contact">NOME</p>
<input id="name" name="name" placeholder="Primeiro e Ășltimo nome" required type="text">
<p class="contact">EMAIL</p>
<input id="email" name="email" placeholder="exemplo#domĂ­nio.com" required type="email">
<p class="contact">MENSAGEM</p>
<textarea rows="6" required placeholder="Escreva aqui a sua mensagem"></textarea>
<br />
<input class="buttom" name="submit" id="submit" tabindex="5" value="ENVIAR" type="submit">
</form>
</div>
And this is my PHP:
<?php
$to = 'MYEMAIL#gmail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = 'From: '.$_POST['email'].'' . "\r\n" .
'Reply-To: MYEMAIL#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $name, $subject, $message, "From: MYEMAIL#gmail.com\n");
?>
Any help, please?
Your form is wrong:
<h2>Feedback Form</h2>
<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"]))
{
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
From: <input type="text" name="from"><br>
Subject: <input type="text" name="subject"><br>
Message: <textarea rows="10" cols="40" name="message"></textarea><br>
<input type="submit" name="submit" value="Submit Feedback">
</form>
<?php
}
else
// the user has submitted the form
{
// Check if the "from" input field is filled out
if (isset($_POST["from"]))
{
$from = $_POST["from"]; // sender
$subject = $_POST["subject"];
$message = $_POST["message"];
// message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// send mail
mail("webmaster#example.com",$subject,$message,"From: $from\n");
echo "Thank you for sending us feedback";
}
}
?>
$name is not a parameter in the mail function. If you are trying to add the persons name somewhere, add it to the subject, message or define it as an additional parameter. Try taking $name out and see if it works without it.
PHP mail function

Php contact form not working well

So I have built my website and I planned on using a php based contact page to send emails to my email address I made this and I receive an e-mail but no content or summary...
so here is my html:
<form action="Php/sendemail.php" method="POST">
<div id="form-inner">
<input type="text" class="input" name="name" id="name" placeholder="Name">
<input type="email" class="input" name="email" id="email" placeholder="E-mail">
<textarea class="input textarea" name="message" id="message" placeholder="Your message here"></textarea>
<input type="submit" class="button" value="Send message" id="button">
</div>
</form>
and here is my php:
$subject = "Contact form submitted!";
$to = 'email#example.com';
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
header('Location: /Contact.php');
?>
I really do not understand why this is happening if you can please help ! Thnaks
This is because you are not geeting the value from the $_POST superglobal:
$body = <<<HTML
$message
HTML;
should be:
$message = $_POST['message'];
$body = <<<HTML
$message
HTML;
Of course there are other ways to do this like just putting $_POST['message']; in the message body but this illustrates how to get the submitted form values.
(You will also want to do things like clean up (i.e. trim(), etc) the submitted values and also defend against header injections as well).

PHP Contact Form that shows success and error Message on the same page

I have created a PHP contact form on my site which works well except to see the Success or Error message it sends users to a the thank-you.php page. That is how I have it set up but I want users to submit and the form should dissapear and say Thank You! in place of the form.
Here is my HTML for the form:
<form class="form-foot" action="thank-you.php" method="post">
<fieldset>
<legend>Footer contact form</legend>
<div class="field">
<label for="name">Your name: </label>
<input name="name" id="name" type="text" placeholder="Name" class="required field-name" required>
</div>
<div class="field">
<label for="email">Your email: </label>
<input name="email" id="email" type="email" placeholder="Email address" class="required field-email" required>
</div>
<div class="field">
<label for="message">Message: </label>
<textarea name="message" id="message" cols="30" rows="8" class="required field-message" placeholder="Your message" required></textarea>
</div>
<div class="field-submit">
<input class="sendbutton" type="submit"/>
</div>
</fieldset>
</form>
And here is my PHP in the thank-you.php page:
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "myemail#mysite.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
How can I make the form disappear on submission and echo the Thank You success message without going to thank-you.php or refreshing the page.
Thanks
Replace:
<form class="form-foot" action="thank-you.php" method="post">
with:
<form name="myform" class="form-foot" action="" method="post">
Give a name to your submit button:
<input name="submit" class="sendbutton" type="submit"/>
Move the code from thank-you.php to yourform.php:
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "myemail#mysite.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
}
you can use ajax to post data to the thank-you.php file, from javascript. by calling a function on submit instead of the action attribute in the form. the function can have the below format
var url = "thank-you.php";
var data = $('#your-form-id').serialize();
$.ajax({
type: "post",
url: url,
data: data,
success: function(msg) {
}
});
On getting the message from the thank-you page you can make use of it to assign it the html containing the form to replace the form with thank you!

cantact form php file doesn't work(Jquery Magical contact form - tutsgeek)

i have a form that work with jquery this form is :
download link for this is
http://www.4shared.com/rar/qkIP2Ulb/magical-contact-form.html
<form class="fcf-contact-form" action="mail.php" method="post">
<div class="right">
<input type="text" name="name" id="name" class="input">
<input type="text" name="email" id="email" class="input">
<div class="left">
<textarea name="message" cols="0" rows="5" class="textarea"></textarea>
<input type="submit" name="submit" value="Send message !" class="submit"/>
</div>
</div>
</form>
and mail.php file code is that code :
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n email: $email \n message: $message";
$recipient = "milad.esmailzade#yahoo.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "<a href='your page link here.php' style='text-decoration:none;color:#ff0099;'></a>";
?>
i upload this contact form on the host and when i click send i dont recieve email from this form !
what is the problem ?
You have to ask your hosting technical support why the PHP mail function does not work?
Try setting SMTP and user parameters, like this:
ini_set("SMTP","mail.mydomain.com" ); // or the IP
ini_set('sendmail_from', 'Me#mydomain.com'); // Account used to deliver the messages.

Categories