user2372214
user2372214

Reputation:

Sending data changed when it received

I have one textarea. I am sending the value of text area

<textarea class="php" name="codeguru"></textarea></div>
<div class="hint">This code is editable. Click Run to execute.</div>
<input type="submit" value="Run" />

through $ajax method

$.ajax({
    type: 'GET',
    url: 'exec.php',
    dataType: 'JSONP',
    data: {
        code: code
    },
    success: function (data) {},
    jsonpCallback: 'mycallback',
    error: function (xhr, ajaxOptions, thrownError, err, textStatus) {

    }
});

Problem : When i am send a data like echo 'sanjay'; it is converted to echo \'sanjay'.

I have same implemented it on localhost and on cpanel. This works fine on localhost but not perfectly on cpanel. Any suggestions or ideas will be appreciated.

Upvotes: 6

Views: 76

Answers (2)

user2372214
user2372214

Reputation:

I got my answer

$code = stripslashes($code);

Because of this code i am able to remove blackslashes

Upvotes: 3

eric.itzhak
eric.itzhak

Reputation: 16072

You should URL Encode it before the AJAX and decode it server side. This happens alot to me with specific chars in JSON strings, I usually URL encode it :

Meaning :

    $.ajax({
    type: 'GET',
    url: 'exec.php',
    dataType: 'JSONP',
    data: {
        code: encodeURIComponent(code)
    },
    success: function (data) {},
    jsonpCallback: 'mycallback',
    error: function (xhr, ajaxOptions, thrownError, err, textStatus) {

    }
});

And in server side :

$code = urldecode($_POST['code']); // or rwaurldecode, not sure

Try sending code with / and " and see how it responds, because I had issues with those as well, if it does try adding slashes to quotes, etc. I use the following function, but you can modify it to suit your needs :

function addslashes( str ) {
    return (str + '').replace(/[\\"]/g, '\\$&').replace(/\u0000/g, '\\0');
}

Upvotes: 0

Related Questions