简体   繁体   English

php jquery 远程验证问题

[英]php jquery remote validate problem

  <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"
        }
    }
});
});

My verifyCap.php我的 verifyCap.php

<?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";
}
 ?>

My problem might due to the response format it is not true or false, but i print out whole verifyCap code.我的问题可能是由于响应格式不正确或不正确,但我打印出整个 verifyCap 代码。 Anyone can help?任何人都可以帮忙吗?

An ajax request does an get by default instead of a post.默认情况下,ajax 请求执行获取而不是发布。 Change:改变:

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

to

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

besides that, do not use $_REQUEST to get your data.除此之外,不要使用$_REQUEST来获取您的数据。 Use $_GET instead.改用$_GET

You could also add some settings your ajax request:您还可以在 ajax 请求中添加一些设置:

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

You're receiving the whole verifyCap.php code because your PHP isn't interpreted by your web server.您收到整个 verifyCap.php 代码,因为您的 PHP 不会被您的 web 服务器解释。

In your verifyCap.php you are using the short tag notation ( <? //code?> ).在您的 verifyCap.php 中,您使用的是短标签符号( <? //code?> )。 Not all server uses this php extension, and it is considered deprecated.并非所有服务器都使用此 php 扩展,它被视为已弃用。 If your webserver doesn't use this extension, then your code is considered as an XML document, as XML document always start with <? <?-- some XML here --> ?>如果您的网络服务器不使用此扩展,那么您的代码将被视为 XML 文档,因为 XML 文档始终以<? <?-- some XML here --> ?> <? <?-- some XML here --> ?> . <? <?-- some XML here --> ?>

Use <?php //code?> and your problem should be fixed.使用<?php //code?>你的问题应该得到解决。

Also, following @XpertEase answer isn't a bad idea either.此外,遵循@XpertEase 的回答也不是一个坏主意。

Edit: More info on PHP short tags Are PHP short tags acceptable to use?编辑:有关 PHP 短标签的更多信息PHP 短标签可以使用吗? (via @XpertEase) (通过@XpertEase)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM