检查返回的文本是真的还是假
问题描述:
我有一个Ajax/PHP代码,我想检查返回的文本是真还是假:检查返回的文本是真的还是假
$(".gets").click(function() {
// some details
$.ajax({
type: "POST",
url: "step2.php",
data: dataString,
success: function(data) {
// This is the button to hide
$('.nums').fadeOut('fast');
} // end function
}); // end ajax
return false;
});
step2.php
if($num < 3) {
echo 'please choose at least 3 nums<br />';
$hideButton = false; // hide show nums button
} else {
$hideButton = true;
}
在此先感谢
答
您必须回显$hideButton
。这些信息将可再data
if($num < 3) {
echo 'please choose at least 3 nums<br />';
$hideButton = false; // hide show nums button
} else {
$hideButton = true;
}
echo $hideButton;
然后 - 改变你的脚本像下面
$.ajax({
type: "POST",
url: "step2.php",
data: dataString,
success: function(data) { // data now has the value of $hideButton
if(data == 'true') {
// This is the button to hide
$('.nums').fadeOut('fast');
}
} // end function
}); // end ajax
答
$(".gets").click(function() {
// some details
$.ajax({
type: "POST",
url: "step2.php",
data: dataString,
success: function(data) {
if(data == 'true')
{// your if code
}
else
{// your else code
}
// This is the button to hide
$('.nums').fadeOut('fast');
} // end function
}); // end ajax
return false;
});
答
尝试。
在AJAX success
success: function(data) {
if(data == 'false'){
$('.nums').fadeOut('fast');
}else{
// other event if you want
}
在step2.php
echo ($num < 3) ? 'false' : 'true';
+0
它将在警告框中返回step2.php的代码,step2.php以
开头,依此类推 – 2014-08-28 17:33:14
但警报(数据);返回第2步的完整代码 – 2014-08-28 17:25:36
然后它听起来像PHP没有运行。你在网络服务器上做了这个吗?还是你在本地加载文件? – 2014-08-28 17:27:19
PHP没有运行!!!!!怎么来的???我没有使用json,我使用的是PHP/Ajax,所以如果我打电话给step2.php,我会收到step2.php中的完整代码(使用alert和console) – 2014-08-28 17:29:46