联系表单给出错误,不触发动作php脚本


Contact form gives error, does not trigger action php script

我知道这个问题可能已经有答案了,但我只是找不到我得到的具体案例,而且现在已经有一段时间了。

那么,我的联系人表单是这样的:

 <div id="contact-form">
    <div id="message"></div>
    <form method="POST" action="php/contact.php" name="contactform" id="contactform">
        <input name="name" type="text" id="name" class="inputForm2" onclick="this.select()" value="Name">
        <input name="email" type="text" id="email" onclick="this.select()" value="E-mail">
        <textarea name="comments" id="comments" onclick="this.select()">Message</textarea>
        <input type="submit" class="send_message transition" id="submit" value="Send Message">
    </form>
</div>

和php脚本

function isEmail($email) { Skipped this one to save space }
if (!defined("PHP_EOL")) define("PHP_EOL", "'r'n");
$name     = $_POST['name'];
$email    = $_POST['email'];
$content = $_POST['comments'];

if(trim($name) == '') {
    echo '<div class="error_message">Enter your name.</div>';
    exit();
} else if(trim($email) == '') {
    echo '<div class="error_message">Enter a valid email address.</div>';
    exit();
} else if(!isEmail($email)) {
    echo '<div class="error_message">You have enter an invalid e-mail address, try again.</div>';
    exit();
} else if(trim($content) == '') {
    echo '<div class="error_message">Enter your message.</div>';
    exit();
} 
if(get_magic_quotes_gpc()) {
    $comments = stripslashes($comments);
}
$emailTo = "x@y";
$subject = 'Youve been contacted by ' . $name . '.';
$e_body = "You have been contacted by: $name" . PHP_EOL . PHP_EOL;
$e_reply = "E-mail: $email'r'nPhone: $phone";
$e_content = "Message:'r'n$comments" . PHP_EOL . PHP_EOL;
//$content = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($emailTo, $subject, $content, $headers)) {
    echo "<fieldset>";
    echo "<div id='success_page'>";
    echo "<h3>Email Sent Successfully.</h3>";
    echo "<p>Thank you <strong>$name</strong>, your message has been submitted to us.</p>";
    echo "</div>";
    echo "</fieldset>";
} else {
    echo 'ERROR!';
}

你能告诉我这有什么问题吗?

首先确保你的php文件夹中有contact.php,还要检查你的html表单是否到达php代码,因为我可以在你的html表单中看到onclick方法。如果你的html表单到达php,那么

您在isEmail($email)函数中有问题。如果您使用isemail函数,那么您必须返回true,因为您在验证中检查else if(!isEmail($email)) {,请按如下更新

function isEmail($email) { 
     return filter_var($email, FILTER_VALIDATE_EMAIL);
     }

完整代码
  function isEmail($email) { 
         return filter_var($email, FILTER_VALIDATE_EMAIL);
         }
if (!defined("PHP_EOL")) define("PHP_EOL", "'r'n");
$name     = $_POST['name'];
$email    = $_POST['email'];
$content = $_POST['comments'];

if(trim($name) == '') {
    echo '<div class="error_message">Enter your name.</div>';
    exit();
} else if(trim($email) == '') {
    echo '<div class="error_message">Enter a valid email address.</div>';
    exit();
} else if(!isEmail($email)) {
    echo '<div class="error_message">You have enter an invalid e-mail address, try again.</div>';
    exit();
} else if(trim($content) == '') {
    echo '<div class="error_message">Enter your message.</div>';
    exit();
} 
if(get_magic_quotes_gpc()) {
    $comments = stripslashes($comments);
}
$emailTo = "e.lucinskas@hotmail.co.uk";
$subject = 'Youve been contacted by ' . $name . '.';
$e_body = "You have been contacted by: $name" . PHP_EOL . PHP_EOL;
$e_reply = "E-mail: $email'r'nPhone: $phone";
$e_content = "Message:'r'n$comments" . PHP_EOL . PHP_EOL;
//$content = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($emailTo, $subject, $content, $headers)) {
    echo "<fieldset>";
    echo "<div id='success_page'>";
    echo "<h3>Email Sent Successfully.</h3>";
    echo "<p>Thank you <strong>$name</strong>, your message has been submitted to us.</p>";
    echo "</div>";
    echo "</fieldset>";
} else {
    echo 'ERROR!';
}