if语句产生错误的结果

时间:2011-07-21 08:17:14

标签: php ajax json

我试图了解为什么这个if语句没有产生正确的消息。应该发生的是,如果用户选择未在数据库中标记为“In”的行,则显示第一条消息。如果他们忘记选择地址或服务级别,则可以触发其他。如果所有情况都正确,则执行else语句。但是,正在发生的事情是第一次选择第一条消息并触发错误时触发第一条消息。如果用户忘记选择地址或服务,则会触发该错误。如果用户满足所有条件,则不显示else语句,即使条件为真,也会显示$ boxstatus错误。有人可以指出我的错误吗?非常感谢

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: application/json");
$json = "";
if ($boxstatus!="In") {
  $json .= "{\"Error\": \"ERROR: The box must be in the archive to enable a retrieval.\"}";
}
else if ($service == '' || $address == '') {
  $json .= "{\"Error\": \"ERROR: You must select a retrieval address and a service level.\"}";
}
else {
$json .= "{\n";
$json .= "\"address\": \"".$address."\",\n";
$json .= "\"service\": \"".$service."\",\n";
$json .= "\"box\": \"".$box."\"\n";
//$json .= "box: [\"". implode('","', $box) ."\"]\n";
$json .= "}\n";
}
echo $json;

ajax如果有帮助:

$.ajax({
               type: "POST",
               dataType: "json",
               url: "boxretrieve.php",
               data: "items="+itemlist+columns,
               success: function(data){
               if (data.Error) jAlert(data.Error);
                    else {
                jAlert("You have successfully retrieved\n\rBox: "+custref+"\n\r"+
                                 "Address: "+address+"\n\r"+
                                 "Service: "+service+"\n\r"+
                                 "Destroydate: "+destroydate);
               $("#flex1").flexReload();
               }
               }
             });

+++++ UPDATE +++++++

将第二个错误更改为Error2并在ajax中执行了一个条件,现在一切正常。感谢大家的帮助。

2 个答案:

答案 0 :(得分:2)

您永远不会为$boxstatus分配值,因此永远不会"In"

(另外,通过粉碎字符串来构建JSON,在组合适当的数据结构后使用json_encode)。

答案 1 :(得分:1)

我已经通过设置变量尝试了代码。有用!条件没有错。您可能需要检查sql查询的结果或在浏览器上显示它的方式。

<?php
$boxstatus = 'In';
$service = '';
$address = '';
$json='';

if ($boxstatus!="In") {
  $json .= "{\"Error\": \"ERROR: The box must be in the archive to enable a retrieval.\"}";
}
else if ($service == '' || $address == '') {
  $json .= "{\"Error\": \"ERROR: You must select a retrieval address and a service level.\"}";
}
else {
$json .= "{\n";
$json .= "\"address\": \"".$address."\",\n";
$json .= "\"service\": \"".$service."\",\n";
$json .= "\"box\": \"".$box."\"\n";
//$json .= "box: [\"". implode('","', $box) ."\"]\n";
$json .= "}\n";
}

echo $json;
?>

输出:

{"Error": "ERROR: You must select a retrieval address and a service level."}

我还使用了您的jQuery,我刚刚使用简单jAlert更改了您的alert。它工作正常。

<script type="text/javascript" src="jquery.js">
</script>
<script type="text/javascript">
$(window).load(function(){
    $.ajax
    ({
       type: "POST",
       dataType: "json",
       url: "test.php",
       success: function(data)
       {
           if (data.Error) 
           {
                alert(data.Error);
            }
            else 
            {
                alert("You have successfully retrieved\n\rBox: "+custref+"\n\r"+
                                 "Address: "+address+"\n\r"+
                                 "Service: "+service+"\n\r"+
                                 "Destroydate: "+destroydate);
               $("#flex1").flexReload();
           }
       }
    });
});
</script>

我不知道jAlert,但请尝试使用alert告诉我结果。

相关问题