Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.


Results 1 to 4 of 4

Thread: After sending email, problem with displaying message

  1. #1

    After sending email, problem with displaying message

    Hi all,

    I've got a script that checks form data, sends email, all the usual stuff. The problem comes what to do after the emailing. The usual thing i do is to send the user to a new page, but this time, it isnt an option. I need to display a message on the same page (with the same url).

     // Set email, subject and style the email message
     $to = "to-someone@example.com";
     $subject = "Some subject type thing"; 
     $headers = "From: " .$_POST['email'];
     $message .= "Name: " . $_POST['name'] . "\n"; 
     $message .= "Email: " . $_POST['email'] . "\n"; 
     $message .= "Phone: " . $_POST['phone'] . "\n";
     if (mail($to,$subject,$message,$headers)) { 
     $endMessage == "<p>Thank You. We have recieved your request and will be in contact shortly.</p>"; // success
     } else { 
     $endMessage == "<p>Unfortunatly, your request could not be completed. Please try again.</p>"; // error
    This doesnt work, however, the following works and as I've got the right functions to display the relevent info on the page, I know its something to do with the above code.

     if (mail($to,$subject,$message,$headers)) { 
      header('Location: success.php'); // success
     } else { 
     header('Location: error.php'); // error
    For your info, here is the simple function that im using to display the $endMessage
     if (!isset($endMessage)) {
    <!-- some html stuff here -->
    <?php }
    else {
    echo $endMessage;
    How come it doesnt work, and is there any way round it?

    Any help would be appreciated

  2. #2
    It doesn't work because $endMessage isn't being memorized or being submitted to the new page. Add this to the top of your script: session_start(); and set the $endMessage like this: $_SESSION['endMessage'] = 'Your text here'; and on the final pages do echo $_SESSION['endMessage'];.

  3. #3
    Ah right.

    Just had a quick try but i get some headers errors. But i dont think it will work anyway, as the form itself is in a drop down box thats generated through javascript with an onclick action on an image. When submitting the form, it of course refreshes the page and the drop down form disappears.

    I think I will have to think this through, and try and incorporate ajax into the page to prevent the page refresh and thus keeping the form in view.

  4. #4
    jwilliam's Avatar
    An intellectual carrot!
    You're using the wrong operator to assign to $endMessage.

    == is for comparisons

    = is for assignment

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Home About kirupa.com Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012