简体   繁体   中英

Modify php mail script to display a popup message and redirect to another page on the website

I have a form that when submitted sends an email using the inbuilt php mail() function.

How do I either:

  1. Display a pop-up message to confirm the form was submitted, and the email sent.

or

  1. Redirect to another page once complete

I would prefer option 1.

if($_POST) {   
    $name = trim(stripslashes($_POST['contactName']));
    $email = trim(stripslashes($_POST['contactEmail']));
    $subject = trim(stripslashes($_POST['contactSubject']));
    $contact_message = trim(stripslashes($_POST['contactMessage']));

    // Check Name
    if (strlen($name) < 2) {
        $error['name'] = "Please enter your name.";
    }
    // Check Email
    if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
        $error['email'] = "Please enter a valid email address.";
    }
    // Check Message
    if (strlen($contact_message) < 15) {
        $error['message'] = "Please enter your message. It should have at least 15 characters.";
    }
    // Subject
    if ($subject == '') { $subject = "Contact Form Submission"; }


    // Set Message
    $message .= "Email from: " . $name . "<br />";
    $message .= "Email address: " . $email . "<br />";
    $message .= "Message: <br />";
    $message .= $contact_message;
    $message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";

    // Set From: header
    $from =  $name . " <" . $email . ">";

    // Email Headers
    $headers = "From: " . $from . "\r\n";
    $headers .= "Reply-To: ". $email . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


    if (!$error) {
        ini_set("sendmail_from", $siteOwnersEmail); // for windows server
        $mail = mail($siteOwnersEmail, $subject, $message, $headers);
        if ($mail) { echo "ok. (Here pop up saying its submitted and redirects)"; }
        else { echo "Something went wrong. Please try again."; }
    } # end if - no validation error
    else {
        $response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
        $response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
        $response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
        echo $response;
    } # end if - there was a validation error
}

?>

When you submit the form, you start a new request so PHP can output any html/javascript you like; you could also just tell it to redirect to a new page.

So you can easily implement either option, for displaying a popup, all you need to do is output the relevant javascript.

Option 1 - "Popup"

If you mean a simple alert dialog that pops up, then the javascript you would need to output is alert('Email sent successfully') - if you mean opening a new window then it would be window.open('newpage.html')

Either way this you would need to wrap the command in script tags:

echo "<script>alert('Email sent successfully');</script>";

This should be placed either in your <head> or <body> tags.

Option 2 - Redirect

When you say redirect, I'm not sure if this is necessary, but it depends where you have placed your code. After sending the email you could choose to output completely different HTML, therefore making your new page. You would need to make sure that your email code happens before any output from your old page. eg

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    //process form and send email
    $mail = mail($siteOwnersEmail, $subject, $message, $headers);
    if ($mail) { 
        include 'emailsent.php';
    }else{
        $include 'emailerror.php';
    }
}else{
    include 'form.php'
}

You could replace the includes with a series of echos or break out of php

if ($mail) { 
    ?><html>...</html><?php
}

if you really want to redirect you can use the header function to send the Location header:

header('Location: emailsent.html');

See php docs for this here: http://php.net/manual/en/function.header.php

If you do this you must make sure you do it before any output has started (echo/print etc.) - this also includes any content before your php eg <html><?php would cause errors like this:

Warning: Cannot modify header information - headers already sent by (output started at /some/file.php:12) in /some/file.php on line 23

You can find more info on this here: How to fix "Headers already sent" error in PHP

You could add a timer event. Set it for about 20 seconds.

Set the timer Echo your message sent If timer =20

Header(Location Then use an if statement to PHP timer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM