1

我有一个使用 AJAX 提交表单和 PHP 的联系表单字段。

我有一个小问题。我不断收到一条错误消息,指出“ the request was successful, but the form was not filled out correctly.”这适用于“ email”字段。表单未提交,用户必须重新填写表单。

不知何故,输入的任何电子邮件地址都是无效的。

有谁知道为什么会这样?

/* Email Variables */
$emailSubject = 'You have received an inquiry from your website';
$webMaster = 'info@3elementsreview.com';

/* Data Variables */
$first = Trim(stripslashes($_POST['First']));
$last = Trim(stripslashes($_POST['Last']));
$email = Trim(stripslashes($_POST['E-Mail']));
$message = Trim(stripslashes($_POST['Message']));
$tempt = $_POST['tempt'];
$tempt2 = $_POST['tempt2'];

if ($tempt == 'http://' && empty($tempt2)) {

    $error_message = '';
    $reg_exp = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9­-]+\.[a-zA-Z.]{2,5}$/";

    if(!preg_match($reg_exp, $email)) {

        $error_message .= "<p>A valid email address is required.</p>";
    }
    if (empty($first)) {
        $error_message .= "<p>Please provide your first name.</p>";
    }
    if (empty($last)) {
        $error_message .= "<p>Please provide your last name.</p>";
    }
    if (empty($message)) {
        $error_message .= "<p>Comments box cannot be left empty.</p>";
    }

    if (!empty($error_message)) {

        $return['error'] = true;
        $return['msg'] = "<h3> The request was successful, but the form is not filled out correctly.</h3>".$error_message;
        echo json_encode($return);
        exit();

    } else {

        $return['error'] = false;
        $return['msg'] = "<p>Thank you for contacting us. We definitely value your feedback," .$first .".</p>";
        echo json_encode($return);
    }

} else {

        $return['error'] = true;
        $return['msg'] = "<h3>There was a problem while sending this form. Try it again.</h3>";
        echo json_encode($return);
}

HTML

    <form class="contact-me" action="mail/mailform.php" method="post" name="contact-me">

<div id="response"><!-----------------CONTAINS FORM ERROR MESSAGE--------------></div>
<input name="First" class="required" title="Your First Name is Required" id="first" autofocus placeholder="First Name" type="text" maxlength="15"></input><br>
<input name="Last"  title="Your Last Name is Required" id="last" placeholder="Last Name" type="text" maxlength="15"></input><br>
<input name="E-mail"  title="E-Mail Address is Required" id="email" placeholder="Email Address" type="email" maxlength="50"></input><br>
<textarea name="Message"  title="Comments are Required" placeholder="Comments" rows="1" cols="50" id="message" onKeyDown="textCounter(this.form.comments,this.form.countDisplay);"onKeyUp="textCounter(this.form.message,this.form.countDisplay);"></textarea><br>
<input type="hidden" name="tempt" id="tempt" value="http://" />
<input type="hidden" name="tempt2" id="tempt2" class="clear" value="" />
<input name="countDisplay" type="text" id="countdisplay" value="2000" size="4" maxlength="4" readonly></input><br>
<button class="button" ontouchstart="('touchstart');">SEND</button>
</form>
4

1 回答 1

0

问题是您的电子邮件输入名称属性。

您的输入是:

<input name="E-mail" title="E-Mail Address is Required" id="email" placeholder="Email Address" type="email" maxlength="50">

并且您正在使用$_POST['E-Mail'](大写M)访问它。请注意,数组索引区分大小写。

更改该行以匹配您输入的名称,如下所示:

$email = Trim(stripslashes($_POST['E-mail']));
于 2013-08-23T17:05:52.363 回答