好的,所以我有一个用户填写的表单,它们是用户名字段和颜色验证字段。当焦点改变时,用户名字段会自动检查我的数据库,以提醒用户他们是否使用了用户名。颜色验证字段将屏幕上显示的图像的颜色与用户输入的颜色进行比较。如果这两个字段都成功完成,则显示要注册的按钮。这是我使用一起使用ajax,jQuery和php进行实时表单验证
$(document).ready(function()
{
$("#username").blur(function()
{
//remove all the class add the messagebox classes and start fading
$("#msgbox2").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
//check the username exists or not from ajax
$.post("checkusername.php",{ username:$(this).val() } ,function(data)
{
if(data=='no') //if username not avaiable
{
$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
{//alert(data);
//add message and change the class of the box and start fading
$(this).html('Your username was taken<? $_SESSION['test2'] = 0; ?>').addClass('messageboxerror') .fadeTo(900,1);
});
}
else
{
$("#msgbox2").fadeTo(200,0.1,function() //start fading the messagebox
{ //alert(data);
//add message and change the class of the box and start fading
$(this).html('User name is available!<? $_SESSION['test2'] = 1; ?>').addClass('messageboxok').fadeTo(900,1);
});
}
});
});
});
jQuery的上面的代码PHP文件是:
session_start();
require("../db.php");
$username = $_POST['username'];
$sql ="SELECT * FROM user WHERE username = '$username'";
$go = mysql_query($sql,$db);
$numrows = mysql_num_rows($go);
if ($numrows) {
// no
echo("no");
} else {
// yes
echo "yes";
}
颜色的第二个字段设置相同:
$(document).ready(function()
{
$("#color").blur(function()
{
//remove all the class add the messagebox classes and start fading
$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
//check the color is right or not
$.post("checkcolor.php",{ color:$(this).val() } ,function(data)
{
if(data=='no') //if color is incorrect
{
$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
{//alert(data);
//add message and change the class of the box and start fading
$(this).html('Your color was incorrect<? $_SESSION['test1'] = 0; ?>').addClass('messageboxerror') .fadeTo(900,1);
});
}
else
{
$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
{ //alert(data);
//add message and change the class of the box and start fading
$(this).html('Verification Successful<? $_SESSION['test1'] = 1; ?>').addClass('messageboxok').fadeTo(900,1);
});
}
});
});
});
以上代码的php文件是:
session_start();
$sescolor= $_SESSION['color'];
$usercolor = $_POST['color'];
$_SESSION['usercolor']= $_POST['color'];
$usercolor = strtoupper($usercolor);
if ($sescolor==$usercolor) {
echo("yes");
} else {
echo "no";
}
我想要发生的是两个字段“用户名”和“颜色”根据他们的状态来改变'test1'和'test2'的会话变量。所以如果用户名无效,test2将被分配一个0而不是1.同样用于颜色验证。然后,我的想法是,我会有一个单独的函数检查,看'test1'和'test2'是否都是1.如果是,用户会看到提交按钮,如果没有,他们会看到一条错误消息。
这是“checkboth.php”会是什么样
$test1= $_SESSION['test1'];
$test2= $_SESSION['test2'];
echo $test1;
echo $test2;
if(($test1 ==1) && ($test2 ==1))
{
echo("yes");
}
else{
echo("no");
}
我使用同样的jQuery的结构的第三个功能。然而,这种方式有效,我总是回复'是'。在会话的var_dump上,我发现test1和test2总是等于0.我该如何去完成这个任务? 在此先感谢!
与jQuery/php异步执行服务器端验证的最简单方法之一是使用$ .getJSON。虽然它可能不适合你如何构建你的应用程序,重构采取这种方法并不是那么耗时。下面是一篇很好的文章,以帮助您开始:http://php4every1.com/tutorials/jquery-ajax-tutorial/ – MoarCodePlz