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 …

via PHP Website Development » Search Results » ajax:

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 retrievednrBox: “+custref+”nr”+
“Address: “+address+”nr”+
“Service: “+service+”nr”+
“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.
……………………………………..

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.
$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.

I don’t know jAlert, but try using alert and tell me the result.
……………………………………..

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).

For more info: if statement producing incorrect result

PHP Website Development » Search Results » ajax

if statement producing incorrect result

Share this post:

Related Posts

Leave a Comment