将jQuery validate插件与"remote"结合使用选项 [英] Using the jQuery validate plugin with the "remote" option
问题描述
我正在使用jQuery validate插件及其远程选项来检查输入字段中是否存在用户名.我使用以下代码:
I am using the jQuery validate plugin and its remote option for checking the existence of a username from an input field. I use this code:
remote : {
url :'ajax_php/admin/checking.php',
type :'post',
data : {
type:'username'
}}
即使我将类型请求设置为post
,我也注意到请求URL附加了回调参数:
I've noticed that the request URL has a callback parameter appended, even though I set the type request to post
:
http://localhost/lopoli2/ajax_php/admin/checking.php?callback=jQuery15104128487491980195_1311232389069
我的PHP脚本可以正常工作,并为有效的用户名返回true
,并为无效的用户名返回一个字符串.但是没有错误消息出现!简而言之,我的PHP文件如下所示:
My PHP script works fine and returns true
for valid username and a string for invalid usernames. But no error message appears! My PHP file in short looks like this:
$check = mysql_query("select `username` from `user_tbl` where `username`='".$_POST['username']."' ",$conn)
or die('Error In DB !');
if (mysql_num_rows($check)>0){
echo("username is already exists");
}else{
echo("true");
}
这就是我想知道的:
- 回调参数有什么用?
- 如何解决显示错误消息"问题?
推荐答案
返回的值被认为是布尔值(true/false)或字符串(将被处理为false并显示返回的文本).
The returned value is considered to be a boolean (true/false) or a string (will be handled as false and display the returned text).
同样重要的是,使用header('content-type:text/plain');
Also, it is important to set the output to text/javascript or text/plain using header('content-type:text/plain');
因此在php中,返回值时,应注意返回值的类型,如下所示:
So in php, when returning the value, you should pay attention to the type of the returned value as follows:
echo'true'; //=> true,一个布尔值. 说验证的结果是真实的,一切都很好.
echo 'true'; //=> true, a boolean. Says the result of the validation is true and everything is okay.
回显'false'; //=> false,一个布尔值. 说验证结果是错误的,并显示在验证器对象中配置的消息
echo 'false'; //=> false, a boolean. Says the result of the validation is wrong and display a message configured in the validator object
回显'"false"'; //=>"false",一个字符串. 说验证结果是错误的,并且显示返回的字符串将显示为错误.
echo '"false"'; //=> "false", a string. Says the result of the validation is wrong and display the returned string will be displayed as an error.
这篇关于将jQuery validate插件与"remote"结合使用选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!