How can I run this contact form without reloading or changing the page on submit? I was told to use ajax but I don't know where to start with that.
I would like to be able to display the output ('Successfully sent! Thank you.') on my index.html inside a div for example.
<?php if (isset($_POST["contact"])){
$to = 'contact#example.com'; //Contact Email
$from = $_POST["email"];
$name = $_POST["name"];
$message = $_POST["message"];
// subject
$subject = 'New message from '.$name;
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: '.$from . "\r\n";
// Mail it
if(mail($to, $subject, $message, $headers)){
echo "Successfully sent! Thank you.";
} else {
echo "Oops! Failed to send your message";
}
}
?>
Try the below codes =)
html
<form id="contactform" name="contactform" method="post" action="email.php">
<div>
<input type="text" name="name">
</div>
<div>
<input type="email" name="email">
</div>
<div>
<input type="text" name="subject">
</div>
<div>
<textarea name="message"></textarea>
</div>
<button type="submit">
Send Message
</button>
</form>
ajax
var form = $('#contactform');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div></div>');
$.ajax({
type:"post",
url: $(this).attr('action'),
data: $(this).serialize(),
beforeSend: function(){
form.prepend( form_status.html('<p>Sending</p>').fadeIn() );
}
}).done(function(data){
form_status.html('<p>Done</p>').delay(3000).fadeOut();
});
});
email.php
<?php
$to = 'contact#example.com'; //Contact Email
$from = $_POST["email"];
$name = $_POST["name"];
$message = $_POST["message"];
// subject
$subject = 'New message from '.$name;
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: '.$from . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
Related
Recently I've been having problems with my PHP contact form. It's worked great for about two years, and I haven't changed anything, so I don't really understand what the problem is. Here's the code:
<?php
// Check for header injections
function has_header_injection($str) {
return preg_match ( "/[\r\n]/", $str );
}
if(isset ($_POST['contact_submit'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$tel = trim($_POST['tel']);
$msg = $_POST['message'];
// check to see if name or email have header injections
if (has_header_injection($name) || has_header_injection($email)){
die();
}
if ( !$name || !$email || !$msg ) {
echo '<h4 class="error">All Fields Required</h4>Go back and try again';
exit;
}
// add the recipient email to a variable
$to = "example#example.net";
// Create a subject
$subject = "$name sent you an email";
// construct your message
$message .= "Name: $name sent you an email\r\n";
$message .= "Telephone: $tel\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
$message = wordwrap(message, 72);
// set the mail header
$headers = "MIME=Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "\r\nFrom: " . $name . " \r\n\r\n" . $tel . " \r\n\r\n " . $msg . "\r\n\r\n <" . $email . "> \r\n\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: high\r\n\r\n";
// Send the Email
mail( $to, $subject, $message, $headers );
?>
<!--- END PHP CONTACT FORM -->
<!-- Show Success message -->
<h2>Thanks for contacting Us!</h2>
<p align="center">Please allow 24 hours for a response</p>
<p>« Go to Home Page</p>
<?php } else { ?>
<form method="post" action="" id="contact-form">
<label for="name">Your Name</label>
<input type="text" id="name" name="name">
<label for="tel">Your Phone Number</label>
<input type="tel" id="tel" name="tel">
<label for="email">Your Email</label>
<input type="email" id="email" name="email">
<label for="message">the date/time you wish to sign up for</label>
<textarea id="message" name="message"></textarea>
<br>
<input type="submit" class="button next" name="contact_submit" value="Sign Up">
</form>
<?php } ?>
However, when the contact form is submitted, instead of sending the information to the body of the email, it sends it in the "From" section of the email. For example, the email might say:
To: Web Developer
From: Bob Smith 888-888-8888 mondays, wednesdays fridays
Subject: Bob Smith sent you an email!
Body:
X-Priority: 1X-MSMail-Priority: high
message
I don't really know what's going on, so any help would be appreciated!
You are adding all that info in the "from" header.
$headers .= "\r\nFrom: " . $name . " \r\n\r\n" . $tel . " \r\n\r\n " . $msg . "\r\n\r\n <" . $email . "> \r\n\r\n";
Change your headers to this:
$headers = "MIME=Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: {$name} <{$email}>\r\n"; // Removed all extra variables
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: high\r\n";
and it should work.
You are already sending the $message, containing all the above data in the body as well.
Why you haven't experienced this before is however a mystery.
NOTE: You only need to have one \r\n after each header.
You should also change this row:
$message = wordwrap(message, 72);
to
$message = wordwrap($message, 72); // Adding $ in front of the variable.
I have a php contact form on my website. However when my customers input their e-mail address and submit their message I got a reply to my own e-mail. For example if they enter adam#yahoo.com I recieve the e-mail as myusername#myhostingprovider.com .... How can I fix this so the reply section shows the e-mail the customer input? Thank you.
Here is my code for my contact form
<form action="php/contact.php" method="post" class="contactForm" id="contactForm">
<fieldset>
<div class="formValidationError bg-red-dark color-white" id="contactNameFieldError">
<p class="center-text uppercase small-text">Name is required!</p>
</div>
<div class="formValidationError bg-red-dark color-white" id="contactEmailFieldError">
<p class="center-text uppercase small-text">Mail address required!</p>
</div>
<div class="formValidationError bg-red-dark color-white" id="contactEmailFieldError2">
<p class="center-text uppercase small-text">Mail address must be valid!</p>
</div>
<div class="formValidationError bg-red-dark color-white" id="contactMessageTextareaError">
<p class="center-text uppercase small-text">Message field is empty!</p>
</div>
<div class="formFieldWrap">
<label class="field-title contactNameField" for="contactNameField">Name:<span>(required)</span></label>
<input type="text" name="contactNameField" value="" class="contactField requiredField" id="contactNameField"/>
</div>
<div class="formFieldWrap">
<label class="field-title contactEmailField" for="contactEmailField">Email: <span>(required)</span></label>
<input type="text" name="contactEmailField" value="" class="contactField requiredField requiredEmailField" id="contactEmailField"/>
</div>
<div class="formTextareaWrap">
<label class="field-title contactMessageTextarea" for="contactMessageTextarea">Message: <span>(required)</span></label>
<textarea name="contactMessageTextarea" class="contactTextarea requiredField" id="contactMessageTextarea"></textarea>
</div>
<div class="formSubmitButtonErrorsWrap">
<input type="submit" class="buttonWrap button button-grey contactSubmitButton" id="contactSubmitButton" value="SUBMIT" data-formId="contactForm"/>
</div>
</fieldset>
</form>
Also here is the code for the PHP script
<?php
// variables start
$name = "";
$email = "";
$message = "";
$name = trim($_POST['contactNameField']);
$email = trim($_POST['contactEmailField']);
$message = trim($_POST['contactMessageTextarea']);
// variables end
// email address starts
$emailAddress = 'myemail#yahoo.com';
// email address ends
$subject = "Mywebsite.com | Mobile - Message From: $name";
$message = "<strong>From:</strong> $name <br/> <strong>E-Mail:</strong> $email </br><br/> <strong>Message:</strong> $message";
$headers = 'From: '. $name . '<' . $email . '>' . "\r\n";
$headers = 'Reply-To: ' . $email . "\r\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//send email function starts
mail($emailAddress, $subject, $message, $headers);
//send email function ends
?>
In your code, you haven't been adding to your header string, you've just been resetting it each time.
What you're finally sending to the mail function in the header variable is just;
'Content-type: text/html; charset=iso-8859-1' . "\r\n"
What you need to do is either where you set $headers = , change the equals sign so it looks like this $headers .=
As that means that it will add on to the String. Or, just format it as one massive string.
<?php
$headers = 'From: '. $name . '<' . $email . '>' . "\r\n";
$headers .= 'Reply-To: ' . $email . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//send email function starts
mail($emailAddress, $subject, $message, $headers);
I need to have the visitor of my website send form data (such as a contact form to my email via PHP. How will I be able to do this?
With GET/POST query and using tag
html forms / w3schools
for example
.html page:
<form method="GET" action="send.php">
<input name="fieldname" type="text" placeholder="You text here...">
<input type="submit" value="Submit">
</form>
send.php
<?php
if (isset($_GET['fieldname']) {
// you code here..
}
example send email by function mail()
about mail() function on php.net
$from = 'fromemailsend#mail';
$to = 'emailtosend#mail';
$subject = 'your subject';
$message = 'your<br>message<br>in html code';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "Content-type: text/html; charset=utf-8 \r\n";
$headers .= 'To: Author <' .$to . ' >' . "\r\n";
$headers .= 'From: '.$author.' <'.$from.'>' . "\r\n";
mail($to, $subject, $message, $headers);
echo '<div id="email">
<form action="#" method="POST">
<label>E-mailadres:</label>
<p><input type="text" name="mail1" value="me#me.nl"> </p>
<input type="submit" name="submitemail">
</form>
</div>';
$to = 'MY#MAIL.COM';
$lala = $_POST['mail1'];
// subject
$subject = 'Subject';
// message
$message = $selected . $totaal .'';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $lala . ' <' . $lala . '>';
// Mail it
mail($to, $subject, $message, $headers);
Sending the email is working fine, it just wont let catch the inserted email.
I can't get the value of <input .... name="mail1"> (me#me.nl) into the "FROM:" section.
What do i wrong OR what is the thing that i don't do in this case ?
Whenever using $headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n"; it works perfectly.
Try
echo '<div id="email">
<form action="a.php" method="POST">
<label>E-mailadres:</label>
<p><input type="text" name="mail1" value="me#me.nl"> </p>
<input type="submit" name="submitemail">
</form>
</div>';
if (isset($_POST['submitemail'])) {
$to = 'MY#MAIL.COM';
$lala = $_POST['mail1'];
// subject
$subject = 'Subject';
// message
$message = $selected . $totaal . '';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $lala . ' <' . $lala . '>';
// Mail it
mail($to, $subject, $message, $headers);
}
the a.php is the name of ur php file
I'm having some problems with my contact page.
Here are the parts:
<form method="post" action="mail.php">
<input name="nome" type="text" style="width: 265px;" placeholder="Nome e Cognome">
<input name="mail" type="email" style="width: 263px;" placeholder="E-mail">
<textarea name="messaggio" placeholder="Messaggio"></textarea>
<button type="submit" name="invia" style="margin-left: 0; margin-top: 10px;">Invia</button>
</form>
and..
<?php
$to = "mail";
$subject = "Modulo proveniente dal sito www.miosito.it";
$body = "Contenuto del modulo:\n\n";
$body .= "Nome: " . trim(stripslashes($_POST["nome"])) . "\n";
$body .= "Email: " . trim(stripslashes($_POST["mail"])) . "\n";
$body .= "Messaggio: " . trim(stripslashes($_POST["messaggio"])) . "\n";
$headers = "From: Valle srl <info#vallesrl.com>";
"Content-Type: text/html; charset=iso-8859-1\n";
if(#mail($to, $subject, $body, $headers)) {
header("Location: http://www.alessandrogiordano.me/test/valle02/sent.php");
} else {
header("Location: http://www.alessandrogiordano.me/test/valle02/nosent.php");
}
?>
First of all.. if I click on the submit button with all blank the email is sent blank.
Even if I make some errors and I get the else message the email is sent.. blank obviously.
I'm going crazy.. I'm making this website for free for a friend but I'm a graphic designer not a web developer. Never again! :D
Help!! Thank you very much.
put this line in your headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Before sending mail you have to validate all the fields whether they are empty or not
Like this
if( trim($_POST["nome"]) != "" )
{
// send mail
$isMailSend = mail($to, $subject, $body, $headers);
}
{
//show error
}
if( $isMailSend ) {
header("Location: http://www.alessandrogiordano.me/test/valle02/sent.php");
} else {
header("Location: http://www.alessandrogiordano.me/test/valle02/nosent.php");
}
This
$headers = "From: Valle srl <info#vallesrl.com>";
"Content-Type: text/html; charset=iso-8859-1\n";
Should be,
$headers = "From: Valle srl <info#vallesrl.com>";
$headers. = "Content-Type: text/html; charset=iso-8859-1\n";
you can't use type="email",there should be type="text"