errorare
errorare

Reputation: 178

Ajax POST Requests return int instead of string

I have the following JS code:

var dataString = "action=validateUsername"+
         "&username="+usernameRegi+
         "&lang="+lang;
$.ajax({
type: "POST",
url: "function.php",
data: dataString,
cache: false,
success: function(result)
    {           
        var expl=result.split("|");

                if(expl[0]=="1")
            alert("1");

                else if(expl[0]=="99")
        alert("99");
    }
});

This is my function.php

if($_POST["action"]=="validateUsername")
{
    $username=$_POST["username"];
    $lang=$_POST["lang"];

    $sqlSelect = "SELECT * FROM user where userName='".$username."'";
    $sqlQuery = mysql_query($sqlSelect);
    $rowCount = mysql_num_rows($sqlQuery);

    if($rowCount>0)
    {
    if($lang=="BM")
        echo "99|My msgA.";
    else
        echo "99|My msgB.";
    }
    else
    {
    if($lang=="BM")
        echo "1|My msgC.";
    else
        echo "1|My msgD.";  
    }
}

The problem is, my ajax request never alert 1 or 99 on success. And I found the problem is expl[0]==1 instead of expl[0]=="1".

When I turn my code to this one, it run smooth.:-

if(expl[0]==1)
alert("1");

else if(expl[0]==99)
alert("99");

This is happen only when I upload my code to the server. No problem on the localhost. What is the problem, is there any setting on the server that cause that problem?

Could anybody explain me, what is happening here?

Upvotes: 1

Views: 1181

Answers (2)

Ganesh Bora
Ganesh Bora

Reputation: 1153

while passing values and returning them from function make sure not to add quotes to int values. or add function below to check / convert them to int

function isint( $mixed ) { return ( preg_match( '/^\d*$/' , $mixed) == 1 ); }

Upvotes: 0

Uchit Shrestha
Uchit Shrestha

Reputation: 547

It's because:

When you assign a numeric value to a variable, do not put quotes around the value. If you put quotes around a numeric value, it will be treated as text.

Upvotes: 4

Related Questions