I have created a contact form and it does work but now I want to display "Message Sent" after submitting the form right below the "Send" button.
HTML file
<form action="send_mail.php" id="footer-form" method="post" role="form">
<div class="form-group has-feedback">
<label class="sr-only" for="name2">Name</label>
<input class="form-control" id="name2" name="name2" placeholder="Name" required="" type="text" />
</div>
<div class="form-group has-feedback">
<label class="sr-only" for="email2">Email address</label>
<input class="form-control" id="email2" name="email2" placeholder="Enter email" required="" type="email" />
</div>
<div class="form-group has-feedback">
<label class="sr-only" for="message2">Message</label>
<textarea class="form-control" id="message2" name="message2" placeholder="Message" required="" rows="8"></textarea>
</div>
<div class="6u 12u$(small)">
<input id="copy" name="copy" type="checkbox" />
<label for="copy">Email me a copy of this message</label>
</div>
<input class="btn btn-default" type="submit" value="Send" />
</form>
PHP Code (different file)
<?php
$name = $_POST['name2'];
$email = $_POST['email2'];
$message = $_POST['message2'];
$formcontent ="From: $name \nMessage: $message";
$recipient = "hello@domain.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>
Just as Funk Forty Niner said:
Use a if/else statement:
<?php
$name = $_POST['name2'];
$email = $_POST['email2'];
$message = $_POST['message2'];
$formcontent ="From: $name \nMessage: $message";
$recipient = "hello@domain.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if(mail($recipient, $subject, $formcontent, $mailheader)) {
die("Error!");
} else {
echo "Success!";
}
?>
if/else
Documentation: http://php.net/manual/en/control-structures.elseif.php
You can simply redirect your php to your html with a parameter like success=true after your mail() function
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: index.php?success=true');
rename your .html to .php
and in your html (now php) under the
<input class="btn btn-default" type="submit" value="Send" />
<?php
if ( isset($_GET['success']) && $_GET['success']==true )
echo "Message Sent";
?>
Real simple, use a conditional statement with an if/else
.
if( mail($recipient, $subject, $formcontent, $mailheader) ){
echo "Sent";
} else {
echo "Error, check your logs";
}
Plus, if your entire code is in the same file, you best be using a conditional statement for all inputs/POST arrays and checking for empty()
fields.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.