簡體   English   中英

PHP / AJAX / Jquery注釋表單驗證

[英]PHP / AJAX / Jquery Comment Form Validation

我正在基於jquery驗證插件(http://docs.jquery.com/Plugins/Validation)創建評論表單。 我知道javascript很容易被黑客操縱,因此我想知道如何通過php進行驗證以確保評論表單不會產生大量垃圾郵件?

js直接來自驗證插件。 html表單直接來自JS驗證插件頁面。 其他js如下:

  <script>
  $(document).ready(function(){
  $("#commentForm").submit(function(){
    if($("#commentForm").validate()){
        $.ajax({
            type: 'POST',
            url: 'process.php',
            data: $(this).serialize(),
            success: function(returnedData){
                alert(returnedData);
            }
        });
    }
    return false;
});
});
  </script>

<form class="cmxform" id="commentForm" method="POST" action="process.php">
   <label for="cname">Name</label>
   <input id="cname" name="name" size="25" class="required" minlength="2" />
   <label for="cemail">E-Mail</label>
   <input id="cemail" name="email" size="25"  class="required email" />
   <label for="curl">URL</label>
   <input id="curl" name="url" size="25"  class="url" value="" />
   <label for="ccomment">Your comment</label>
   <textarea id="ccomment" name="comment" cols="22"  class="required"></textarea>
<input class="submit" type="submit" value="Submit"/>

php目前是非常標准的。 不確定如何將驗證與js和ajax集成:

<?php

$to      = 'sdfsadfssfasd@gmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com';

mail($to, $subject, $message, $headers);

print "Form submitted successfully: <br>Your name is <b>".$_POST['cname']."</b> and your email is <b>".$_POST['email']."</b><br>";
?>

謝謝你的幫助。 有人評論說這很模糊。 澄清:

我了解如何使用php來驗證電子郵件長度,不必要的字符等。我不了解jquery驗證插件如何通過ajax工作。 我需要知道如何配置我的php條件,以正確驗證注釋表單以防止垃圾郵件。

好的,您這樣做的方式非常簡單。 因此,您具有提交功能,但是在實際將請求發送到.php文件之前,您需要驗證輸入。 到目前為止,您的代碼還算不錯,但是您需要從validate函數開始。 看起來像這樣(在jQuery中)。 假設您的輸入字段具有以下ID:#input1,#input2,#input3,#input4。

    function validate()
{
    if($('#input1).val()=='' || $('#input2).val()==''|| $('#input3).val()=='' $('#input4).val()=='') return 0;
    else return 1;

}

這將是使用驗證的最基本方法。 如果您不希望使用jQuery,則可以將$('#input1)替換為docoument.getElementById(老式方式)。 如果您還想使其對用戶看起來不錯,則可以在模糊狀態下驗證每個輸入字段! 例如,假設用戶專注於第一個輸入字段#input1

$('#input1).focus(function(){change the background color,add a border, or anything you want just to let the users see which input they clicked/tabbed on.}).blur(function(){check if the input is valid and if it's not display a message or anything you want.});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM