表单弹出消息

时间:2011-12-25 20:05:45

标签: javascript forms popup

我在我的网站上使用此表单处理器。目前,当用户点击提交时,他们将被带到新页面以显示错误消息或表单已发送的确认。我希望错误和确认消息显示为与表单本身相同的页面上的弹出窗口。非常感谢任何帮助。

<?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();
}
?>

1 个答案:

答案 0 :(得分:1)

尝试类似:

表单处理器

/* 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']);
}