[英]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"
}
}
});
});
我的 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";
}
?>
我的问题可能是由于响应格式不正确或不正确,但我打印出整个 verifyCap 代码。 任何人都可以帮忙吗?
默认情况下,ajax 请求执行获取而不是发布。 改变:
if ($_SERVER ["REQUEST_METHOD"] != "POST")
至
if ($_SERVER ["REQUEST_METHOD"] != "GET")
除此之外,不要使用$_REQUEST
来获取您的数据。 改用$_GET
。
您还可以在 ajax 请求中添加一些设置:
type: "POST",
data: "your params here",
您收到整个 verifyCap.php 代码,因为您的 PHP 不会被您的 web 服务器解释。
在您的 verifyCap.php 中,您使用的是短标签符号( <? //code?>
)。 并非所有服务器都使用此 php 扩展,它被视为已弃用。 如果您的网络服务器不使用此扩展,那么您的代码将被视为 XML 文档,因为 XML 文档始终以<? <?-- some XML here --> ?>
<? <?-- some XML here --> ?>
。
使用<?php //code?>
你的问题应该得到解决。
此外,遵循@XpertEase 的回答也不是一个坏主意。
编辑:有关 PHP 短标签的更多信息PHP 短标签可以使用吗? (通过@XpertEase)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.