使用jquery的Php查询
我正在处理一个网页,当用户选中复选框时,调用一个PHP函数来查询表(如果用户存在)。如果是,则显示按钮以转到下一页 到目前为止我已经试过了。我确信我的php工作正常。当用户存在时,我检查了结果变量返回0,但由于某些原因,它没有执行使用jquery的Php查询,php,jquery,html,ajax,Php,Jquery,Html,Ajax,我正在处理一个网页,当用户选中复选框时,调用一个PHP函数来查询表(如果用户存在)。如果是,则显示按钮以转到下一页 到目前为止我已经试过了。我确信我的php工作正常。当用户存在时,我检查了结果变量返回0,但由于某些原因,它没有执行if语句 $(document).ready(function() { $('#submit').hide(); $('#mobiletask').change(function(){ if($('#mobiletask').attr
if
语句
$(document).ready(function() {
$('#submit').hide();
$('#mobiletask').change(function(){
if($('#mobiletask').attr('checked'))
{
check_availability();
// $( "#notifyresult" ).html( "<p>Awesome, we'll send you an email!</p>" );
}
});
});
//function to check username availability
function check_availability(){
//get the username
// var username = $('#username').val();
var username = '<?php echo $_GET['id']; ?>';
$.post("check_username.php", { username: username }, function(result){
//if the result is 1
if(result == 1){
//show that the username is available
$("#errormessage").html('<p>PLease complete the task before proceeding</p>');
}
else if(result == 0) {
//show that the username is NOT available
$('#submit').show();
}
});
}
$(文档).ready(函数()
{
$(“#提交”).hide();
$('mobiletask')。on('change',function(){
if($('mobiletask').attr('checked'))
{
检查可用性();
//$(“#notifyresult”).html(太棒了,我们会给你发封电子邮件!”;
}
});
功能检查_可用性()
{
//获取用户名
//var username=$('#username').val();
var用户名=“”;
$.ajax({
url:“check_username.php”,
键入:“POST”,
async:false,
数据:{'name':'username','value':username},
成功:功能(结果)
{
警报(结果);
//如果结果是1
如果(结果='1')
{
//显示用户名可用
$(“#errormessage”).html(“请在继续之前完成任务””;
}else if(结果='0')
{
//显示用户名不可用
$(“#提交”).show();
}
},错误:函数(错误)
{
警报(错误);
}
});
}
});
**完成代码测试后删除警报** 试试这个:
$.post("check_username.php", { username: username }, function(result){
//if the result is 1
if(result == '1'){
//show that the username is available
$("#errormessage").html('<p>PLease complete the task before proceeding</p>');
}
else if(result == '0') {
//show that the username is NOT available
$('#submit').show();
}
});
$.post(“check_username.php”,{username:username},函数(结果){
//如果结果是1
如果(结果='1'){
//显示用户名可用
$(“#errormessage”).html(“请在继续之前完成任务””;
}
如果(结果='0'){
//显示用户名不可用
$(“#提交”).show();
}
});
根据您得到的响应:
<html><body>1</body></html>
1
你需要做的是处理你的PHP文件,并确保删除其中的任何HTML。你得到的错误是什么?我没有得到任何错误,只是没有执行if和else条件。请指导我,我是ajax和jquery新手,试图在我发出警报时了解你在
check_username.PHP
中的回声(结果)它显示1非常感谢您,但如果和else不执行,则是同一问题。请帮助您可以指导我如何执行。我尝试了htmlspecialchars,但没有运气确保您的PHP目标页面中没有HTML代码。您可以使用以下示例:
。非常感谢..修复了it@maddy我很乐意帮忙。
$.post("check_username.php", { username: username }, function(result){
//if the result is 1
if(result == '1'){
//show that the username is available
$("#errormessage").html('<p>PLease complete the task before proceeding</p>');
}
else if(result == '0') {
//show that the username is NOT available
$('#submit').show();
}
});
<html><body>1</body></html>