0

On my website I have a 'Contact Us' form.

Currently, I am using a php form to have the information that has been filled out on the form to be e-mailed to my e-mail.

I wanted an echo message to come up once someone clicks on submit, but instead it's going to another page with the message there.

I just want the page to refresh.

This is the PHP Code:

     <?php


   //echo $emailBody;
   sendEmail();

 function sendEmail(){
 global $emailBody, $name, $from, $fromName, $templateFile, $to, $subject;

     $headers  = "MIME-Version: 1.0" . "\r\n";
     $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";    
     $headers .= "Reply-To: ". $from . "\r\n";    
     $headers .= "From: ". $from . "\r\n";
     $headers .= "Return-Path: ".$from;      

     if (!mail($to, $subject, $emailBody, $headers)) {

    } else {

    echo "Your Message has been sent!we will contact back you in a short moment!";
    }

}



 ?>
user2913307
  • 1
  • 2
  • 3

5 Answers5

0

PHP will always send you a new page because it is a server side language. In order for this code to be executed when a user clicks the button and then the results shown to the user, the request must be sent to the server, executed, and then sent back to the user. The only way you can 'refresh' a page like you are requesting is to use a client side language such as javascript.

It is like this: When user clicks the submit button they are requesting that PHP process the request. This can only take place on the server. Because the request goes from user to server and then back to user, you will always get a new page.

takendarkk
  • 3,347
  • 8
  • 25
  • 37
  • @garrettlynch please stop making such meaningless edits - they provide no improvement and these questions/answers don't need to be bumped to active state. Instead, you could edit your most recent question to make it fit the site rules. – takendarkk Jan 18 '18 at 17:11
0

Need a little more code, but typically when doing something like this, you would have the form submit back to iself and at the top of the php, do some isset(-form variable -) checking, if they are filled out then you can enter a function to process from there and subsequently echo a message to the user indicating success/failure.

HashHazard
  • 561
  • 4
  • 14
0

I'd use:

<script type="text/javascript">
setTimeout(function(){location.reload();},1000);
</script>
<noscript>
<meta http-equiv="refresh" content="1" />
</noscript>

You can also specify this with a header (the meta-refresh only emulates this header):

header('Refresh: 1');

It is also nice to leave the user a message such as:

<div>If this page doesn't automatically refresh in one second <a href="whatever">click here</a>.</div>
Ultimater
  • 4,647
  • 2
  • 29
  • 43
0

change your action

<form action="This is where your webpage goes" method="POST">

so this will send your page to google:

<form action="http://google.com" method="POST">

if you remove the action, it will reload the current page, like this:

<form method="POST">

just put your php in the same page as your <form>.

like the following

<?php

if($_POST['submit'] == 'send'){ 

     sendEmail();

     function sendEmail(){
     global $emailBody, $name, $from, $fromName, $templateFile, $to, $subject;

         $headers  = "MIME-Version: 1.0" . "\r\n";
         $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";    
         $headers .= "Reply-To: ". $from . "\r\n";    
         $headers .= "From: ". $from . "\r\n";
         $headers .= "Return-Path: ".$from;      

        if (!mail($to, $subject, $emailBody, $headers)) {

        } else {
           echo "Your Message has been sent!we will contact back you in a short moment!";
        }

    }
}

//For that to work your button needs to be like this
?>
<form method="POST">
    <!-- other fields here -->
    <input type="submit" name="submit" value="send" />
</form>
Arian Faurtosh
  • 17,987
  • 21
  • 77
  • 115
0

before you echo you can redirect to the source page with the message that you want to show up there

for example

if (!mail($to, $subject, $emailBody, $headers)) {

    } else {
  $myMsg="Your Message has been sent!we will contact back you in a short moment!";
  header('location: yourpage');
}

and in your page you can check out if there is any message like this

if(isset($myMsg))
{
 echo $myMsg; // in the place & style that you want 
}
merou
  • 95
  • 4