Take the 2-minute tour ×

I'm using the code below to send an email to my address alerting me of a new user sign up:

$errors  = '';
$myemail = 'name@mydomain.com';

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

if (!preg_match(
   )) {

if (empty($errors)) {
    $to            = $myemail;
    $email_subject = "Invitation Request: $email_address";
    $email_body    = "has received a new invitation request. " .
                     "\n Email: $email_address";

    $headers = "From: $myemail\n";

    mail($to, $email_subject, $email_body, $headers);
    //redirect to the 'thank you' page

Currently it redirects users to a new page but instead I want it to allow for a javascript pop-up success/fail message much like those used in modern websites.

Here is the message code (CSS and Javascript):


<style type="text/css">
/* Status Message */
.message { 
    display: none; 
    margin: 0 0 13px 0; 
    padding: 13px 13px 13px 52px; 
    background: url('icon_check.gif') left no-repeat #EFA; 
    background-position: 13px 5px; 
    border: solid 1px #BD8; '
    width: 300px; 


and here's the html for the success message:

<div class="message">Your Invitation Request Has Been Received!</div>

Is it possible to do this? I've looked in several places for the answer over the past two days and I can't seem to change the right thing. Any help would be appreciated.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

With your current setup, the best way to do this would be to redirect back to the signup page with a query string that tells the page to open the window from there.

header("Location: /signup.php?success=true");

and in the JS


if(isset($_GET['success']) && $_GET['success'] == 'true'){
     echo "window.open( '/success.html', 'success', '[options],[..],[..]' );";


Also, I recommend using filter_var to validate emails.

filter_var('bob@example.com', FILTER_VALIDATE_EMAIL) 
share|improve this answer
Thanks for the quick response! Can I put that PHP code anywhere within the php file? Also, the sign up form is on the index page, would I change "signup.php" to "index.php"? I'm completely new at this stuff so I'm a bit lost. –  Sharkbites Apr 24 '12 at 21:31
Put the header() code where you have //redirect to the 'thank you' page. If there is any code after that, add in exit(); to stop the script's execution (but only do this after you have added the new user to your database). You need to change signup.php to index.php. However, I would add your full domain name to it (e.g. http(s)://example.com/?success=true). –  honyovk Apr 24 '12 at 21:36
Ah perfect you fixed the redirect problem...now to figure out how to post a failure message if what's entered is not a proper email address. The code does redirect when whatever is entered into the input box is not a correct email address. –  Sharkbites Apr 24 '12 at 22:01
First, set $errors = ''; to $errors = false;. Then do if($errors) {header("Location: http(s)://example.com/?success=false&description=$errors"); exit();}. and simply open an error page, or better yet just open an alert() with the error –  honyovk Apr 24 '12 at 22:07
add comment

The easiest way to do this so to combine your PHP mail function with your HTML page. You could use AJAX to load the send mail PHP page into a DIV, which will overwrite the content of that DIV with whatever the resulting output of the PHP function is. This would allow you to have a fallback for users without JS since submitting the form to the PHP page would still work.

share|improve this answer
Thank you for your response. I wish I knew how to do any of that. I'm pretty new to this stuff. –  Sharkbites Apr 24 '12 at 21:33
add comment

Your Answer


