'I want to send an email from an html form that shows a popup when the email is sent
I should preface this post with the fact that I am just a hobbyist and am not real code savvy when it comes to PHP and JQuery and such. What I am trying to do is send an email from an HTML form using a PHP script that I found on the internet somewhere. I have this all working just fine, but I dont like the fact that after the email is sent, the user is sent to a new page with a message saying it was sent. Instead of opening a new page, I would like the user to stay on the same page and get a popup message instead. Here is the code I am using for my PHP script:
<?php
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$subject = "Mail from website";
$message = $_POST['message'] ;
$to = "[email protected]";
$headers = "From:" . $email . "\r\n" .
"Reply-To:" . $email;
if(mail($to,$subject,$message, $headers)) {
echo "Your email was sucessfully sent!";
} else {
echo "The email message was not sent.";
}
?>
I did not attach my html since I didnt think it was really needed. Any help on this would be greatly appreciated!
Solution 1:[1]
Redirect to a query string like header("location: formfile?msg=success");
And then in order to pull message just use $parts = parse_url($url); parse_str($parts['query'], $query); $alert_value = $_GET['msg']; in file where your form is located. After that you can do actions with if statements.
Here is example:
the file where form is located
<?php
$parts = parse_url($url);
parse_str($parts['query'], $query);
$alert_value = $_GET['msg'];
if ($alert_value == "success") {
//do your actions to show lighbox.
}
?>
<!-- Your code for form in html --->
And your file where you send email:
<?php
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$subject = "Mail from website";
$message = $_POST['message'] ;
$to = "[email protected]";
$headers = "From:" . $email . "\r\n" .
"Reply-To:" . $email;
if(mail($to,$subject,$message, $headers)) {
header("location: formfile.php?msg=success");
} else {
header("location: formfile.php?msg=error");
}
?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Valery Dremov |
