如何使用Jquery强制表单提交

时间:2015-01-10 13:45:16

标签: javascript php jquery forms twitter-bootstrap

祝你2015年快乐!

我有一个简单的联系我们PHP表格。我正在用parsley.js验证它。验证工作正常,但我收到了很多垃圾邮件。

我相信如果我只能在启用Jquery的情况下强制提交表单,那么它应该解决我的问题(对吧?)。

我不是PhP / Jquery的专家,我们将不胜感激。

这是我的PHP代码

<?php

// Define Variables i.e. name tag, as per form and set to empty
$contact_name = $contact_email = $contact_phone = $contact_message = "";

// Sanitize data and use friendly names
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $name = test_input($_POST["contact_name"]);
  $email = test_input($_POST["contact_email"]);
  $phone = test_input($_POST["contact_phone"]);
  $message = test_input($_POST["contact_message"]);
}
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

// Set values
$to = 'info@foryourservice.in';
$subject = 'New Message from Website';
$headers = 'From: info@domainname.com' . "\r\n" .
   'Reply-To: info@domainname.com' . "\r\n" .
   'X-Mailer: PHP/' . phpversion();

// Set Email content           
$emailcontent = "A new message has been submitted from the website.
\n
Name : $name
Email : $email
Phone : $phone
Message : $message";

// Mail function
$send_contact=mail($to,$subject,$emailcontent,$headers);

if($send_contact){
header('Location: index.php#contactusform');
}
else {
echo "<script type='text/javascript'>alert('We encountered an ERROR! Please go back and try again.');</script>";
}

?>

这是我的HTML(我正在使用Twitter Bootstrap)

<form role="form" method="POST" action="contactusform.php" id="contactusform" data-parsley-validate>
<div class="col-xs-6">
    <div class="form-group" style="margin-bottom: -5px">
    <label for="input1"></label>
    <input type="text" name="contact_name" class="form-control" id="input1" placeholder="Name*" required data-parsley-required-message="Please enter your name">
    </div>
        <div class="form-group" style="margin-bottom: -5px">
    <label for="input2"></label>
    <input type="email" name="contact_email" class="form-control" id="input2" placeholder="Email Address*" data-parsley-trigger="change" required data-parsley-required-message="Please enter a valid Email address">
    </div>
    <div class="form-group" style="margin-bottom: -5px">
    <label for="input3"></label>
    <input type="tel" name="contact_phone" class="form-control" id="input3" placeholder="Phone Number*" required data-parsley-type="digits" data-parsley-minlength="10" data-parsley-maxlength="10" data-parsley-required-message="Please enter a 10 digit number">
        </div>
    <br>
    <div class="form-group">
    <button type="submit" id="contactbutton" class="btn btn-primary" style="background-color: #A8B645; border-color: transparent">Submit</button>
    </div>
</div>
<div class="col-xs-6">
    <div class="form-group">
    <label for="input4"></label>
    <textarea name="contact_message" class="form-control" rows="7" id="input4" placeholder="Message*" required required data-parsley-required-message="Say something!"></textarea>
        </div>
</div>
</form>

这就是Spam Email的样子:

该网站已提交新邮件。

姓名:&amp;#12362;&amp;#36023;&amp;#12356;&amp;#24471;&amp;#12450;&amp;#12490;&amp;#12473;&amp;#12452; &安培;#12511;&安培;#12525;&安培;#12540;&安培;#12489;&安培;#22823;&安培;#22771;&安培;#12426;&安培;#20986;&安培;#12375;&安培;#12521; &安培;#12531;&安培;#12461;&安培;#12531;&安培;#12464; 电子邮件:rsilau@gmail.com 电话:&amp;#12362;&amp;#36023;&amp;#12356;&amp;#24471;&amp;#12450;&amp;#12490;&amp;#12473;&amp;#12452; &安培;#12511;&安培;#12525;&安培;#12540;&安培;#12489;&安培;#22823;&安培;#22771;&安培;#12426;&安培;#20986;&安培;#12375;&安培;#12521; &安培;#12531;&安培;#12461;&安培;#12531;&安培;#12464; 消息:购物者在袋子上吹嘘 &安培;#12362;&安培;#36023;&安培;#12356;&安培;#24471;&安培;#12450;&安培;#12490;&安培;#12473;&安培;#12452; &安培;#12511;&安培;#12525;&安培;#12540;&安培;#12489;&安培;#22823;&安培;#22771;&安培;#12426;&安培;#20986;&安培;#12375;&安培;#12521; &安培;#12531;&安培;#12461;&安培;#12531;&安培;#12464; http://www.frkapaun.org/dyqfmnwg/ysl-annasuixmraAekm.asp

2 个答案:

答案 0 :(得分:0)

在表单中添加隐藏字段:

<input type="hidden" value="0" id="botcheck" name="botcheck" />

然后使用jQuery将值设置为1

$("#botcheck").val("1");

然后服务器端检查$_POST["botcheck"]的值。

答案 1 :(得分:0)

您可能想要检查您的表单是否使用ajax提交:

 if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  //Process form here
 }

For further explanation