Deepeshkumar
Deepeshkumar

Reputation: 443

Why the php 'if statement' does not give expected result?

I am designing a php application using AJAX and PHP. But the if statement in my php file behaves unexpectedly. The request method is 'POST'; Ajax code is

        function fetchData(){
            var recipename = document.getElementById("recipe").value;
            var calorie = false;
            createRequestObject();
            //window.alert(calorie);
            var url = "reciepinfo.php"; 
            xmlHttp.open("POST",url,true);
            xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            window.alert(calorie);
            xmlHttp.send("recipe="+recipename+"&calorie="+calorie);
            xmlHttp.onreadystatechange = function(){
                if(xmlHttp.readyState===4 && xmlHttp.status===200){                       
                    document.getElementById("target").innerHTML = xmlHttp.responseText;                                          
                }
            }
        } 

And php code is:

  <?php
 $_SESSION['flag']=false;
if($_SESSION['flag']==false){
    session_start(); 
    $_SESSION['flag']=true;
}
$recipename = $_REQUEST['recipe'];
$calorie = $_REQUEST['calorie'];  
 echo $calorie;
 $calorietemp = 0;
 //echo $recipename;
 $database = "nutrition";
 $link = mysqli_connect('localhost','root','',$database);
 $query = "select * from recipestb where name='$recipename'";
 //$result = mysqli_query($link,$query);
 //$obj = mysqli_fetch_object($result);;
 if($link){
    $result = mysqli_query($link,$query);
    $obj = mysqli_fetch_object($result);
    //$arr = mysqli_fetch_array($result);
   //$names = "";
    if(is_object($obj)){
            echo "<i/>";
            echo "<font size='5'>";
            echo "Recipe name: ".$obj->name;
            echo '<br/>';
            echo "Carbohydrates : ".$obj->carbs." grams";
            echo '<br/>';
            echo "Proteins: ".$obj->protein." grams";
            echo '<br/>';
            echo "Fat: ".$obj->fat." grams";
            echo '<br/>';
            echo "Calories: ".$obj->calorie." cal";
            $calorietemp = $obj->calorie;
            echo '<br/>';    

    }else{
        echo "non object";
    }

 }else{
     echo "Connection failed";
 }
if($calorie==true){
     echo $calorie;
     $_SESSION['caloriecount'] = $_SESSION['caloriecount'] + $calorietemp;           
     echo "Total calorie in diet :".$_SESSION['caloriecount'];          
}

My session handling is weird i accept, but neglecting that, even the variable calorie is explicitly set to false, the if block executes.The echo $calorie statement also executes and it displays $calorie as false. And i am not getting what is going wrong exactly. Almost irritated with this. Can anybody help?

EDIT1: The php code is fine... Ajax code has some problem... When i set the $calorie to false in php code.. It behaved properly..

Upvotes: 1

Views: 71

Answers (2)

Deepeshkumar
Deepeshkumar

Reputation: 443

As a beginner i was unaware that the java-script variable with value false is passed as a string "false" to a server side script like PHP.

If we use a boolval(), in PHP this issue can be resolved. PHP boolval()

I set the $calorie in my java-script to 0 or 1 as per my need. When i wanted the false value to be sent to PHP script ,i set $calorie in java-script to 0 and the boolval() in corresponding PHP script evaluated it as false. So the issue was resolved.

Upvotes: 0

jpaljasma
jpaljasma

Reputation: 1612

Leaving the session handling aside, the $calorie problem ... You pass the data via AJAX as strings.

$calorie = 'false';
var_dump((true == $calorie));

You will get always bool(true). Using your AJAX example above, try this snippet instead (and use POST instead of REQUEST):

$calorie = ('true' == $_POST['calorie']);
var_dump($calorie);
// produces `bool(false)`

Upvotes: 2

Related Questions