PHP表单不起作用

时间:2015-09-02 13:13:57

标签: php jquery html forms

有人可以解释为什么以下PHP表单不起作用。我已尝试提交表单,但收件箱未收到该电子邮件。这是正确的电子邮件地址。

此外,最好使用PHP而不是jQuery来提醒"您的消息已成功发送!"?因为目前它表明即使收件箱没有收到邮件也会发送邮件。如果是这样,怎么样?

HTML:

<div id="formDiv">
        <form id="validationForm">

            <label for="anti" id="antiLabel">This is an anti 's'-to-the-'pam' field. Accordingly, do not delete or enter any information here.</label>
            <input name="anti" id="antiInput"/> 

        <div id="formSection1">
            <label for="name" class="label">Name:</label>
            <input name="name" id="name" class="input" required/> 

            <label for="email" class="label">Email:</label>
            <input name="email" id="email" class="input" required/> 

            <label for="subject" class="label">Subject:</label>
            <input name="subject" id="subject" class="input" required/> 
        </div>

        <div id="formSection2">

            <label for="message" id="labelMessage">Message:</label>
            <textarea name="message" id="inputMessage" required></textarea>

            <input id="submitButton" type="submit" value="Submit" class="input"/>
        </div>

        </form>
    </div>

PHP:

<?php 
$ToEmail = 'jono@s.co.uk'; 
$EmailSubject = 'Site contact form'; 

$mailheader = "From: ".$_POST["email"]."\r\n"; 
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 

$MESSAGE_BODY = "Name: ".$_POST["name"].""; 
$MESSAGE_BODY .= "Email: ".$_POST["email"].""; 
$MESSAGE_BODY .= "Subject: ".nl2br($_POST["subject"]).""; 
$MESSAGE_BODY .= "Message: ".nl2br($_POST["message"]).""; 
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 
?>

jQuery的:

<script>

$("#validationForm").submit(function(event){

        var errorMessage="";

        event.preventDefault();

        function isValidEmailAddress(emailAddress) {
        var pattern = new 
        RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
        return pattern.test(emailAddress);
        };

            /* Regex validation of email address */

        if (!isValidEmailAddress($("#email").val())) {
            errorMessage="*Please enter a valid email address*";}


        if (errorMessage=="") {
            $("#error").html("");
            alert("Your message has been successfully sent!");
        } else {
            $("#error").html(errorMessage);
        }

        if(!String.IsNullOrEmpty(Request.Form["anti"]))
            IgnoreComment();

        });


    </script>

2 个答案:

答案 0 :(得分:1)

 <form id="validationForm"> 

你在哪里定义了行动?

请添加methodaction

 <form id="validationForm" action="yourAction.php" method="post" >

如果您想将其发布在那里,只需添加method='post'

即可

答案 1 :(得分:1)

如果您打算使用$_POST['myField']检索表单值,则需要在method标记中添加正确的<form>属性。

因此,请使用:

<form id="validationForm" method="post">

因为默认情况下为get