我有以下作用于 AJAX 查询的函数:
var formData = $(this).serialize(); //put the form names and values into an array called formdata
$.get('filtertest.php',formData,processData); //jquery ajax call
function processData(data){
if(data =="a"){
$('#content').html("<h2>You have not selected any 'types of dwelling'. Please select at least one.</h2>");
$('#linky').hide();
}
else if(data=="b"){
$('#content').html("<h2>You have not selected any 'style of dwelling'. Please select at least one.</h2>");
$('#linky').hide();
}
else if(data=="c"){
$('#content').html("<h2>You have not selected any 'situation of dwelling'. Please select at least one.</h2>");
$('#linky').hide();
}
else if(data==1){
$('#content').html('<h2>There is ' + data + ' property available!</h2>');
$('#linky').show();
}
else if(data==0){
$('#content').html('<h2>There are no properties available, please expand your search options.</h2>');
$('#linky').hide();
}
else{
$('#content').html('<h2>There are ' + data + ' properties available!</h2>');
$('#linky').show();
}
}//end processData
在filtertest.php文件中,以下代码用于检查是否没有输入任何字段值,如果是则返回字母a:
//if none of the TODs are selected, returns an error
if ($_GET[tod_house]==0 && $_GET[tod_bung]==0 && $_GET[tod_flat]==0 && $_GET[tod_barnc]==0 && $_GET[tod_farm]==0 && $_GET[tod_small]==0 && $_GET[tod_build]==0 && $_GET[tod_devland]==0 && $_GET[tod_farmland]==0 ){
echo "a";
return;
}
但是在最初的ajax函数中processData
该行以 if(data =="a"){
开头没有捕获回显的值 - 它一直到最终的 else
功能。奇怪的是字母'a'在最后的else
中被输入到html中函数,这意味着 echo 是正确的,并且 filtertest.php 文件立即返回 - 它只是没有被 if
捕获字母“a”的功能测试。
这里肯定有一些语法错误,但我在过去一个小时里一直在尝试不同的东西,但我无法解决它,它开始真的让我烦恼!!
<小时/>编辑
更改下面指出的愚蠢错误后,它仍然无法工作。我尝试回显不同的数字而不是字母。我改变了两个 processData
中的“a”和filtertest.php
到“-1”,嘿普雷斯托!有用。所以这是包含“a”的字符串的错误 - 我在 javascript 函数中对两个字符串的比较是否错误?
最佳答案
问题在于您如何尝试访问 $_GET
数组中的数据:键应该是字符串,即
$_GET['tod_house']
不是
$_GET[tod_house]
更多精度来自Marc B' comment :输出不会只是 a
,因此不会被选择。
关于javascript - if 函数未检测到从 php 函数返回的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22056241/