if statement producing incorrect result
I am trying to see why this if statement is not producing the correct message. What is supposed to happen, is that if a user selects a row that is not marked in the db as 'In' then the first message is displayed. If they forget to select an address or service level, then the else if sould be triggered. If all cases are correct, then perform the else statement. However, what is happening, is that the first message is being triggered when it is selected the first time and fires the Error. If a user开发者_如何学运维 then forgets to select an address or service then that Error is fired. If the user then meets all conditions, instead of performing the else statement, it displays the $boxstatus error, even though the conditions are true. Can someone please point out my error? Many thanks
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 if it helps:
$.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+++++++
Changed the second error to Error2 and did a condition in the ajax and all is now well. Thanks to everyone for there help.
You never assign a value to $boxstatus
so it will never be "In"
(Also, don't build JSON by smashing strings together, use json_encode after putting together a proper data structure).
I've tried your code by setting up your variables. It works! Nothing wrong with the conditions. You may want to check the result of your sql query or the way you show it on the browser.
<?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;
?>
The output:
{"Error": "ERROR: You must select a retrieval address and a service level."}
I also used your jQuery, I just changed your jAlert
with simple alert
. It works fine.
<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>
I don't know jAlert
, but try using alert
and tell me the result.
精彩评论