PhP联系表格有效文本检查不起作用

时间:2014-06-10 05:42:03

标签: php html forms

我有这个PhP代码:

<?php
// check for form submission - if it doesn’t exist then send back to contact form
if (!isset($_POST["save"]) || $_POST["save"] != "contact") {
    header("Location: ../../contact.php"); exit;
}
// get the posted data
$name = $_POST["contact_name"];
$email_address = $_POST["contact_email"];
$message = $_POST["contact_message"];
// check that a name was entered
if (emptyempty($name))
    $error = "You must enter your name.";
// check that an email address was entered
elseif (emptyempty($email_address))
    $error = "You must enter your email address.";
// check for a valid email address
elseif (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]    {2,3})$/", $email_address))
    $error = "You must enter a valid email address.";
// check that a message was entered
elseif (emptyempty($message))
    $error = "You must enter a message.";
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
    header("Location: ../../contact.php?e=".urlencode($error)); exit;
}
// write the email content
$email_content = "Name: $name\n";
$email_content .= "Email Address: $email_address\n";
$email_content .= "Message:\n\n$message";
// send the email
mail ("SOMETHING@gmail.com", "New Contact Message", $email_content);
// send the user back to the form
header("Location: ../../contact.php?s=".urlencode("Thank you for your message."));
exit;
?>

但是当它重定向到contact-form-submission.php时,它只会给出一个空白页面。当我删除这个....

// check that a name was entered
if (emptyempty($name))
    $error = "You must enter your name.";
// check that an email address was entered
elseif (emptyempty($email_address))
    $error = "You must enter your email address.";
// check for a valid email address
elseif (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]      {2,3})$/", $email_address))
    $error = "You must enter a valid email address.";
// check that a message was entered
elseif (emptyempty($message))
    $error = "You must enter a message.";
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
    header("Location: ../../contact.php?e=".urlencode($error)); exit;
}

代码工作正常,检查有效文本的代码有什么问题?

2 个答案:

答案 0 :(得分:0)

更改上述代码并移除emptyempty()并添加empty()

// check that a name was entered
if (empty($name))
$error = "You must enter your name.";
// check that an email address was entered
elseif (empty($email_address))
$error = "You must enter your email address.";
// check for a valid email address
elseif (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]      {2,3})$/", $email_address))
$error = "You must enter a valid email address.";
// check that a message was entered
elseif (empty($message))
$error = "You must enter a message.";
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
header("Location: ../../contact.php?e=".urlencode($error)); exit;
}

答案 1 :(得分:0)

emptyempty无效。它应该是空的()。此外,您的电子邮件验证的正则表达式对我不起作用,我建议:

    elseif (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email_address))
$error = "You must enter a valid email address.";