I am using this form processor on my site. Currently when the user hits submit they are brought to a new page to display either the error message or confirmation that the form has been sent. I want the error and confirmation messages to be displayed as a pop-up on the same page as the form itself. Would appreciate any help on this.
<?php
/* Set e-mail recipient */
$myemail = "mail@mail.com;
/* Check all form inputs using check_input function */
$email = check_input($_POST['email']);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "
Someeone has signed up to your newletter.
E-mail: $email
Regards,
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
Try something like:
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
$_SESSION['form_error'] = "E-mail address not valid";
header('location:' . $_SERVER['HTTP_REFERER']);
exit;
}
if(isset($_SESSION['form_error'])) {
echo '<script>alert("' . addslashes($_SESSION['form_error']) . '");</script>';
unset($_SESSION['form_error']);
}
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.