I'm a beginner when it comes to PHP.
This is a 'contact us' form where users can send their questions. Currently, it's successful in sending submitted questions directly to our email. However, it failed to redirect to a thank you page after the submission. I sincerely hope that someone could help me. I'm lost. The codes are below. Thank you in advance.
<?php
// Define some constants
define( "RECIPIENT_NAME", "Choice Baby Enquiry" );
define( "RECIPIENT_EMAIL", "enquiry@choicebaby.com.my" );
// Read the form values
$success = false;
$senderName = isset( $_POST['name'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name'] ) : "";
$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$subject = isset( $_POST['subject'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['subject'] ) : "Enquiry";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";
// If all values exist, send the email
if ( $senderName && $senderEmail && $message ) {
$recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
$headers = "From: " . $senderName . " <" . $senderEmail . ">";
$success = mail( $recipient, $subject, $message, $headers );
}
if ( mail( $recipient, $subject, $message, $headers )) {
header ("Location: thanks.html");
exit ();
}
?>
----EDIT----
Here is the HTML :
<section class="contact_us_container">
<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="text-align:center;"> <!-- section title -->
<h2>Get In Touch With Us</h2>
<p>Type your question here and we will get back to you as soon as possible.</p>
</div> <!-- End section title -->
<div class="col-lg-8 col-md-8 col-sm-12 col-xs-12 form_holder"> <!-- form holder -->
<form action="includes/sendemail.php" class="contact-form">
<input class="form-control name" type="text" name="name" placeholder="Your Name">
<input class="form-control email" type="email" name="email" placeholder="Your Email">
<input class="form-control" type="text" name="subject" placeholder="Subject">
<textarea name="message" placeholder="Your Message"></textarea>
<button type="submit" class="submit">submit now<i class="fa fa-arrow-circle-right"></i></button>
</form> <!-- End form holder -->
</div>
<div class="col-lg-4 col-md-4 col-sm-12 col-xs-12 pull-right address">
<address>
<div class="icon_holder float_left"><span class="icon icon-Pointer"></span></div>
<div class="address_holder float_left">M2-19-03,<br>8trium Tower, Jalan Cempaka SD 12/5,<br>Bandar Sri Damansara,52000 KL</div>
</address>
<address class="clear_fix">
<div class="icon_holder float_left"><span class="icon icon-Plaine"></span></div>
<div class="address_holder float_left">enquiry@choicebaby.com.my</div>
</address>
<address class="clear_fix">
<div class="icon_holder float_left"><span class="icon icon-Phone2"></span></div>
<div class="address_holder float_left">+ 603 6735 5374</div>
</address>
</div>
</div>
</div>
</section>
HTML Code :
<form action="includes/sendemail.php" class="contact-form" method="post">
<input class="form-control name" type="text" name="name" placeholder="Your Name">
<input class="form-control email" type="email" name="email" placeholder="Your Email">
<input class="form-control" type="text" name="subject" placeholder="Subject">
<textarea name="message" placeholder="Your Message"></textarea>
<button type="submit" class="submit" name="btnSubmit">submit now<i class="fa fa-arrow-circle-right"></i></button>
</form>
PHP Code : includes/sendemail.php
<?php
if(isset($_POST["btnSubmit"]))
{
// Define some constants
define( "RECIPIENT_NAME", "Choice Baby Enquiry" );
define( "RECIPIENT_EMAIL", "enquiry@choicebaby.com.my" );
// Read the form values
$success = false;
$senderName = isset( $_POST['name'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name'] ) : "";
$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$subject = isset( $_POST['subject'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['subject'] ) : "Enquiry";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";
// If all values exist, send the email
if ( $senderName && $senderEmail && $message ) {
$recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
$headers = "From: " . $senderName . " <" . $senderEmail . ">";
$success = mail( $recipient, $subject, $message, $headers );
}
if ( mail( $recipient, $subject, $message, $headers ))
{
header ("Location: thanks.html");
exit ();
}
}
?>
Replace with this code..
I will use jquery/ajax like this:
I suppose you have a file where there is your form
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="keywords" content="">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript" src="functions.js"></script>
</head>
<body>
<form role="form" method="post" id="subscribe">
<input type="text" name="name" id="name" value="" placeholder="Name"/>
<input type="email" name="email" id="email" value="" placeholder="Email"/>
<input type="text" name="subject" id="subject" value="" placeholder="Subject"/>
<textarea rows="4" cols="50" name="message" id="message"> </textarea>
<input type="submit" class="button" name="submit" value="Submit">
</form>
<div class="message"></div>
<div class="message-notice"></div>
</body>
</html>
your PHP file where there is your php script which works.(suppose it is called subscribe.php)
<?php
// Define some constants
define( "RECIPIENT_NAME", "Choice Baby Enquiry" );
define( "RECIPIENT_EMAIL", "enquiry@choicebaby.com.my" );
// Read the form values
$success = false;
$senderName = isset( $_POST['name'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name'] ) : "";
$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$subject = isset( $_POST['subject'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['subject'] ) : "Enquiry";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";
// If all values exist, send the email
if ( $senderName && $senderEmail && $message ) {
$recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
$headers = "From: " . $senderName . " <" . $senderEmail . ">";
$success = mail( $recipient, $subject, $message, $headers );
}
if ( mail( $recipient, $subject, $message, $headers )) {
//header ("Location: thanks.html");
echo "success";
exit ();
}else{
echo "fail";
}
?>
What you are missing is this javascript script:
;(function($, window, document, undefined) {
var $win = $(window);
var $doc = $(document);
$doc.ready(function() {
$('#subscribe').submit(function() {
$.ajax({
url: 'subscribe.php',
data: $('#subscribe').serialize(),
type: 'POST',
success: function(msg) {
if(msg=="success"){
$("#subscribe").hide(); //Hide your form
$(".message").show(); //Show the message
$(".message").html('<span style="color:green;">You have successfully subscribed to our mailing list.</span>');
window.setTimeout(function(){
window.location.href = "http://www.yoururl.com/thanks.html";
}, 10000); //After 10seconds you will be redirect to the thanks page.
document.getElementById("subscribe").reset();
} else {
$(".message-notice").fadeIn("slow");
$(".message-notice").html(msg);
}
}
});
return false;
});
});
})(jQuery, window, document);
Hope it helps.
Have you tried placing ob_start()
at the beginning of your code?
Or, how about JavaScript's document.location
?
plus method="post" in the form tag
<form action="includes/sendemail.php" class="contact-form" method="post">