0

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

  • 1
    `&` is a special value in URLs and form encoded data sent via POST. It marks the end of the value of the previous parameter. It seems you are not properly encoding your JSON before you send it. Please post the JavaScript code with which you are sending the data. – Felix Kling Dec 05 '13 at 09:29
  • Please [edit] your question and add your code. It's unreadable in the comments. Format your code properly: http://stackoverflow.com/editing-help. – Felix Kling Dec 05 '13 at 09:50

1 Answers1

1

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.

Community
  • 1
  • 1
kinghfb
  • 1,011
  • 5
  • 14