0

Am new to PHP. Trying to send mail using php. Everything fine. Except success message displays in new page.

My php code is

<?php


$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['message'];


$to='abc@xyz.com';

$headers = 'From: '.$name."\r\n" .
    'Reply-To: '.$email."\r\n" .
    'X-Mailer: PHP/' . phpversion();
$subject = $subject;
$body='You have got a new message from the contact form on your website .'."\n\n";

$body.='Name: '.$name."\n";
$body.='Email: '.$email."\n";
$body.='Subject: '.$subject."\n";
$body.='Message: '."\n".$message."\n";

if(mail($to, $subject, $body, $headers)) {
    die('Message sent.');
} else {
    die('Error: Mail failed');
}

?>

And this is my output. enter image description here Please help me to print this mesage below my contact form.

Tomasz Kowalczyk
  • 10,472
  • 6
  • 52
  • 68
Monicka Akilan
  • 1,501
  • 5
  • 19
  • 42
  • 1
    It is not a new page. `die(..)` will just stop rendering your page as soon as you call it. – putvande Dec 15 '14 at 17:28
  • 1
    That's how http forms work. you have a form, which submits to a URL. Doesn't matter if it's the same url, or a completely different one. – Marc B Dec 15 '14 at 17:30
  • Thanks for your immediate response. even echo() also prints the same result. Can you please suggest me what i have to use here. – Monicka Akilan Dec 15 '14 at 17:30
  • @MonickaAkilan Could you describe where you want the message to be displayed? Do you want it to reload the same page as the form but with a message? – khartnett Dec 15 '14 at 17:33
  • @khartnett yes. I want to refresh my page once the mail got sent. And the result should display in bottom of the form – Monicka Akilan Dec 15 '14 at 17:35

4 Answers4

0

Try this:

if (isset($_POST['submit'])){
       $name=$_POST['name'];
       $email=$_POST['email'];
       $subject=$_POST['subject'];
       $message=$_POST['message'];
       $message = ""; //initialize
   ...

   if(mail($to, $subject, $body, $headers)) {
     $message = 'Message sent.'; //Don't use the die function
   } else {
     $message = 'Error: Mail failed';
   }
}

//Then echo the $message variable further down in the form code in the HTML
echo $message; //` This will be an empty string when you load your form page.

$message will get set when you submit your form.

Mr. Concolato
  • 2,224
  • 5
  • 24
  • 44
  • Thanks for your answer. but i didn't understand this. //Then echo the $message variable further down in the form code..... How to use that? – Monicka Akilan Dec 15 '14 at 17:33
  • It might be worth noting that when the the mail function returns true it does not guarantee that the message was sent, only that it was accepted for delivery. – HeadCode Dec 15 '14 at 18:07
0

Don't use die, just use echo to print the message:

if(mail($to, $subject, $body, $headers)) {
    echo 'Message sent.';
} else {
    echo 'Error: Mail failed';
}
rgettman
  • 176,041
  • 30
  • 275
  • 357
jay.jivani
  • 1,560
  • 1
  • 16
  • 33
0

When your target is a different page from your form, it will redirect the browser to that page with a Post request. In order to stay on the same page, make your target the same page as the form. See here: https://stackoverflow.com/a/5826877/1512654

You will then want to check if at least one of your post variables are set.

if (isset($_POST['email']) {

If they are not set, then you are loading the page for the first time and don't need to send an email. When the new Post request comes in, it will send the email. Use echo to print the message and put this section at the bottom of the form where you want the message to appear.

Community
  • 1
  • 1
khartnett
  • 831
  • 4
  • 14
-1

Check with echo 'Your message goes here'; This should work out.

echo 'To print normal sentences';

print 'Similar to echo';

print_r($array); // To print an array structure

For your problem echo should work. try to avoid die while returning a success message or any message.

sanjeev shetty
  • 438
  • 4
  • 17