Sathishkumar S
Sathishkumar S

Reputation: 35

json_decode with special characters

I'm passing value from java script to php file for insertion, If i send name without special characters there is no problem with insertion.

name: sathish
location: bangalore

But i'm sending my name with any special characters then json_decode() not accepting it.

name: sathish&kumar
location: bangalore

I tried with utf8_encode() and var_dump() function.

In first case var_dump displays
string(43) "{"name":"sathish","location":"bangalore"}"

In second case var_dump displays
string(18) "{"name":"sathish"

It ends with the 18 characters in this case, if i'm missing anything in it. Please help me solve this problem.

JavaScript:

function createTC()
{
    var p=document.forms['TCForm'];
    var JSONObject =new Object;

    JSONObject.name=p['txtName'].value;
    JSONObject.location=p['txtLocation'].value;
    JSONstring = JSON.stringify(JSONObject);

    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        var request = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        var request = new XMLHttpRequest();
    }

    var random1 = Math.random();
    var urlstr="../TraineeCreation/createTC.php?rand="+random1+"&json="+JSONstring;    
    request.open("GET", urlstr , true);

    request.onreadystatechange = function()
    {
        if (request.readyState == 4)
            alert(request.responseText);
    }

    request.send(null);
}

Thanks

Upvotes: 0

Views: 1271

Answers (1)

kinghfb
kinghfb

Reputation: 1021

As you're putting JSON in the query string, the ampersand is splitting the value. You should encode the components properly with encodeURIComponent.

Take a look at this thread.

Upvotes: 1

Related Questions