1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

not leaving the page after sending an email

Discussion in 'PHP' started by LS5000, May 30, 2016.

  1. #1
    So I am trying to get this Ajax type of popup (so far as I've looked for answers, I only found Ajax and JQuery way to do this) thing after sending email through PHP.

    I wish to make a div appear on the same page after I click my 'send' button, but PHP immediately drops me on a blank page right after I click it. I wish it to not leave the page.

    It's one of the simpliest php contact form script I could stumble upon:
    
    <?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $human = $_POST['human'];
    $from = $_POST['email'];
    $to = 'info@solsticegraphics.eu'; //set to the default email address
    $subject = $_POST['subject'];
    $body = "From: $name\n E-Mail: $email\n Message:\n $message";
    
    $headers = "From: $email" . "\r\n" .
    "Reply-To: $email" . "\r\n" .
    "X-Mailer: PHP/" . phpversion();
    
    if(isset($_POST['submit']) && ($_POST['human']) == '4') {   
    mail ($to, $subject, $body, $headers);
    
      // I get a blank page here after sending email...
    
    } 
    ?>
    
    Code (markup):
    Is there a way of making PHP send email, but stay on the same page?
    Is there a way of doing this without Ajax/JQuery and how do I do that?
     
    LS5000, May 30, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Is that PHP-code on the same page as the actual contact form? If not, it's not very strange that you get a blank page... If it IS on the same page, it's probably because of something else entirely, but if it's in its own file, then you need to do a header redirect where you have your comment //blank page right now. basically something like
    
    header('location: contactform.html'); //replace contactform.html with whatever page you want to show
    
    Code (markup):
    But it's probably easier to have the PHP on the same page as the contact form, and when you submit the page and the sending is successfull, show a specific div with "thank you" or whatever.
     
    PoPSiCLe, May 30, 2016 IP
  3. Blizzardofozz

    Blizzardofozz Well-Known Member

    Messages:
    132
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    118
    #3
    You could do it with session messages and you display the success message on the second page.

    I don't know x-mailer but basically if you want to submit the form on the same php page you should do it in action attribute of the form. Then you just get the result of the message sending and if OK (true) then display a message on the same page. If you redirect user to a new page after sending then you should use sessions.

    Could you post the whole php script and the HTML form? I don't see redirect in your script.

    Actually I found a tutorial so it's not x-mailer just simple mail php function.
    
    <?php
    $to = 'maryjane@email.com';
    $subject = 'Marriage Proposal';
    $from = 'peterparker@email.com';
    
    // To send HTML mail, the Content-type header must be set
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    
    // Create email headers
    $headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    
    // Compose a simple HTML email message
    $message = '<html><body>';
    $message .= '<h1 style="color:#f40;">Hi Jane!</h1>';
    $message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
    $message .= '</body></html>';
    
    // Sending email
    if(mail($to, $subject, $message, $headers)){
    echo 'Your mail has been sent successfully.';
    } else{
    echo 'Unable to send email. Please try again.';
    }
    ?>
    
    Code (markup):
    I don't see any redirect so the page is blank because you just don't have any message on it?

    Mail function is not recommended as far as I know. phpmailer is better option.
     
    Last edited: May 30, 2016
    Blizzardofozz, May 30, 2016 IP