0

So I am trying to make redirection after submitting the form to the home page. Fine, it works BUT there is one problem. The message is not displayed as user presses "Submit" button but is instantly redirected to the Home page. How can I make it so users first recieve "Thank you" message and than are redirected to the Home page?

<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];

$mail_to = 'vladozabunov@gmail.com';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
  <script language="javascript" type="text/javascript">
    alert('Thank you for the message. We will contact you shortly.');
    window.location = 'contact_page.html';
  </script>
  <?php
}
else { ?>
    <script language="javascript" type="text/javascript">
      alert('Message failed. Please, send an email to gordon@template-help.com');
      window.location = 'contact_page.html';
    </script>
    <?php
}
header('Location: https://saprs.000webhostapp.com/index.html');
?>
Nimish
  • 1,006
  • 1
  • 7
  • 19

4 Answers4

2

just remove the header from your code and replace with following

$mail_to = 'vladozabunov@gmail.com';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
 <script language="javascript" type="text/javascript">
  alert('Thank you for the message. We will contact you shortly.');
  window.location.href = 'index.html';
 </script>
 <?php
 }else { ?>
  <script language="javascript" type="text/javascript">
   alert('Message failed. Please, send an email to gordon@template-help.com');
   window.location.href = 'contact_page.html';
  </script>
 <?php } ?>
Ankit Singh
  • 1,477
  • 1
  • 13
  • 22
1

Please do it with jQuery and ajax.

HTML and jQuery:

 <!DOCTYPE html>
    <html>
    <head>
        <title>Contact Form</title>
        <script src="//code.jquery.com/jquery-1.9.1.js"></script>
        <script type="text/javascript">
            function submitForm() {
             //Do validation and submit form
                $.ajax({
                  url: "mail.php",
                  type: "POST",               

                }).done(function( data ) {
                    alert(data);
                     if(data==1){
                         alert('Success');
                         window.location.href = 'test.php';//Your location 
                     }
                     else
                     {
                         alert('failed'); 
                     }
                });
                return false;
            }
        </script>
    </head>
    <body>
        <form method="post" id="mailData" name="mailData" onsubmit="return submitForm();">
            <label>Contact Form:</label><br>        
            <input type="submit" value="Submit" />
        </form>
        <div id="output"></div>
    </body>
</html>

Server-side PHP code:

<?php
$mail_to = 'vladozabunov@gmail.com';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { 
   echo 1;
}
else { 
   echo 0;  
}

?>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user1752065
  • 179
  • 9
0
$mail_to = 'vladozabunov@gmail.com';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);
if($mail_status == true){
    echo "<script>alert('Thank you for the message. We will contact you shortly.');</script>";
    header('Location:https://saprs.000webhostapp.com/contact_page.html');
}else{
    echo "<script>alert('Sorry! Please try again.');</script>";
    header('Location:https://saprs.000webhostapp.com/contact_page.html');
}
0

Replace

header('Location: https://saprs.000webhostapp.com/index.html');

with

echo "<script type='text/javascript'>window.location.href='https://saprs.000webhostapp.com/index.html'</script>";
Nagesh Katke
  • 540
  • 7
  • 23
  • Yes but this just shows the message and than somehow disables whole page and show only form, again and nothing else instead of going to home page. –  Jun 19 '17 at 11:13
  • what message it is showing and does mail shooting successfully. – Nagesh Katke Jun 19 '17 at 11:19
  • Shows that the mail was sent, it sends the mail but than redirects to same page again but only form is visible. –  Jun 19 '17 at 11:27
  • then try this `if ($mail_status) { echo ""; } else { echo ""; }` Dont add any redirection after this. – Nagesh Katke Jun 19 '17 at 11:34
  • It says: 404. That’s an error. The requested URL was not found on this server. I am sorry I am really stupid to this, never worked with php before. –  Jun 19 '17 at 11:38
  • try your urls in window.location.href where you want to redirect after mail sent successfully or failed. Try this `if ($mail_status) { echo ""; } else { echo ""; }` Then you will come to know what is going wrong. – Nagesh Katke Jun 19 '17 at 11:40