2011-06-10 90 views
0
<script type="text/javascript"> 
    $().ready(function() { 
jQuery.validator.addMethod("captcha", function(value, element) { 
     $.ajax({ url: "verifyCap.php", 
       type: "GET", 
       data: "txtCaptcha="+value, 
       success:   
        function(msg) { 
         if(msg == "true") 
           return true; // already exists 
         return false; 
         } 
        }); 
    },""); 
// validate signup form on keyup and submit 
$("#signupForm").validate({ 
    rules: { 
     title: "required", 
     contactname: "required", 
     email: { 
      required: true, 
      email: true 
     }, 
     comment: "required", 
     txtCaptcha:{ 
      required: true, 
      captcha: true 
     } 
    }, 
    messages: { 
     contactname: "Please enter your contact name", 
     email: "Please enter a valid email address", 
     comment: "Please enter your system requierment", 
     txtCaptcha: { 
      required:"Please enter verification code", 
      captcha: "The verification code is incorrect" 
     } 
    } 
}); 
}); 

我verifyCap.phpPHP jQuery的遠程驗證問題

<?php 
session_start(); 
if ($_SERVER ["REQUEST_METHOD"] != "GET") 
    die ("You can only reach this page by posting from the html form"); 
if (($_GET ["txtCaptcha"] == $_SESSION ["security_code"]) && (! empty ($_GET ["txtCaptcha"]) && ! empty ($_SESSION ["security_code"]))) { 
    echo "true"; 
} else { 
    echo "false"; 
} 
?> 

我的問題可能是由於響應格式是不是真的還是假的,但是我打印出整個verifyCap代碼。任何人都可以幫忙

+0

這是你與你的形式做的唯一驗證更多信息?或者還有其他服務器端驗證? – KyleWpppd 2011-06-10 11:26:44

回答

0

ajax請求默認獲取,而不是發佈。更改:

if ($_SERVER ["REQUEST_METHOD"] != "POST") 

if ($_SERVER ["REQUEST_METHOD"] != "GET") 

除此之外,不使用$_REQUEST,讓您的數據。改爲使用$_GET

你也可以添加一些設置您的Ajax請求:

type: "POST", 
    data: "your params here", 
+0

**這個** AJAX請求使用'GET',但不是所有請求都這樣做。您也可以將其設置爲「POST」。你應該編輯你的答案以反映這一點,因爲它有點模棱兩可。 – 2011-06-10 09:19:08

+0

awesome edit =) – 2011-06-10 09:21:56

+0

好吧,我改變了兩個GET仍然是相同的問題 – user236501 2011-06-10 09:31:13

0

您收到全verifyCap.php代碼,因爲你的PHP不是由Web服務器解釋。

在您的verifyCap.php中,您使用的是短標記符號(<? //code ?>)。 並不是所有的服務器都使用這個php擴展,並且它被認爲已被棄用。如果您的網絡服務器不使用此擴展名,那麼您的代碼將被視爲XML文檔,因爲XML文檔始終以<? <!-- some XML here --> ?>開頭。

使用<?php //code ?>和你的問題應該修復。

此外,以下@XpertEase答案也不是一個壞主意。

編輯:在PHP短標記Are PHP short tags acceptable to use?(通過@XpertEase)

+0

這應該也有幫助。更多信息在這裏:http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use – 2011-06-10 09:21:31

+0

嗨我已經改變它<?php仍然得到同樣的問題 – user236501 2011-06-10 09:30:09