我有一个switch语句返回true或false,并且if和else语句然后用它来做更多的验证。获取Json返回true或false
因为该值使用ajax在输入字段上发送。我希望PHP返回true或false。
我已经尝试使用JSON,我使用警报来测试我回来了,它只是很多的HTML。
$a = isset($_POST['ccode']) ? $_POST['ccode'] : NULL;
function isValid($Code) {
switch ($Code){
case "123":
case "45545":
return true;
default:
return false;
}
}
if(isValid($a)) {
$correct = "correct";
echo json_decode($correct);
}
else {
$incorrect = "incorrect";
echo json_decode($incorrect);
}
AJAX
$('#ccode').blur(function() {
var code = $(this).val();
$.ajax({
type:'POST',
url: 'index.php',
dataType: 'json',
data:{
ccode:code
},
success: function(data) {
alert(data);
}
});
当你看到警报(数据) 我将与
if (data == false) {
//do this
}
});
来代替它我的代码不会返回true或false从PHP和警报它只是从我的索引页面输出大量的HTML。
我做错了什么?
为什么回显json_decode?它应该是json_encode –
@abbiya谢谢,但我的'如果(数据== true)'不起作用。 – user3057514