I am intercepting a form post using jQuery. With the form fields I am creating a JSON object which is stored in a hidden form field. The value that is passed in to the form field is similar to the following:
{"Status" : "Closed", "Location" : "Glasgow", "Date" : "2012-02-15"}
But if I echo the object from the $_POST variable:
echo $_POST['JSON'];
It output's the following:
{\"Status\" : \"Closed\", \"Location\" : \"Glasgow\", \"Date\" : \"2012-02-15\"}
I have tried running this through stripslashes() and urldecode() but I have had no joy. I understand that I could just replace the back slashes with a replace function but thats a bit too much of a hack.
Has anyone came across this malfored JSON across post before?
Note: This is on the back end of a Wordpress site. I am unsure if that would cause this effect.
Looks like you server has magic_qoutes_gpc 'on'. (http://www.php.net/manual/en/security.magicquotes.what.php)
I came over the same problem once and all I did was using JSON.stringify() to store it as a "String" in my hidden Field and reading the output with jquery.parseJSON() method. Maybe this helps you ! With stringify you can also define a replacer for your JSON Object.
var myJSONText = JSON.stringify(myObject, replacer);
http://www.json.org/js.html
http://api.jquery.com/jQuery.parseJSON/
Although my English is not good, but I see it is the issue of json in php, you can use json_decode do, can be transformed into an array
Another possibility you have is to url-encode with encodeURIComponent() in javascript your json object and urldecode() in php the received object.
Be aware that encodeURIComponent() in js is not exactly the same as urlencode() in php and similarly decodeURIComponent() is not the same as urldecode(), but in most cases encoding in js and decoding in php and vice-versa works well.
Related
I have JSON data called rowdata that I use in an ajax call. If I use:
JSON.stringify(rowdata)
it looks like like the following:
{"Description":"qwerty","Code":"12345","Size":"11","Colour":"green"}
I do send it to php and use a GET statement (Joomla's
JRequest::getVar("Description", "", "", "")
statement) to get the elements of rowdata but I cannot succeed.
If I look at the ajax data that has been send I do have the following:
rowdata%5BDescription%5D=qwerty
etc. after applying:
$.param(data)
I have used many version instead of "Description", but to no avail. I tried to get rowdata on its own and access its elements, but no success. I cannot find out what the %5B and %5D means, searching for that is problematic with the % sign. Anyone who can help to get the values of Description, Code, etc. in php?
You are sending JSON, but trying to parse it as application/x-www-form-urlencoded data.
Don't convert the object to JSON.
$.get('example.php', {"Description":"qwerty","Code":"12345","Size":"11","Colour":"green"});
I believe %5B is [ and %5D is ]. Your URL is encoding special characters. It's called URL encoding.
Do not use JSON if you want to push data with get... otherwise it will be encoded to satisfy JSON format and you will get those entities.
$.get('target.php', {"Description":"qwerty","Code":"12345","Size":"11","Colour":"green"});
I have a javascript which sends some specific information to a PHP api . Before to send it performs encodeURI . How can I "decode" it in PHP ? I understand that urldecode/urlencode is different that javascript encode/decodeURI so what can I use ?
Use encodeURIComponent in Javascript: http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp and urldecode in PHP: http://php.net/manual/en/function.urldecode.php
Unless you've encoded it multiple times (e.g. by explicitly calling the encode method AND inserting the value into a form field which is then submitted) you don't need to do anything - it is transparently converted back to its original form when the request is parsed.
You can use rawurldecode function in php, but this function is not UTF-8, then your have to convert to UTF-8 with utf8_decode like this
echo utf8_decode(rawurldecode('Educa%C3%A7%C3%A3o%20Multim%C3%ADdia'));
i have a case where i have to send an html code from client side to server side through json object ,but there are some problems caused by symbols inside the html tag such as ",/ ...etc which create errors in the json object. I tried many ways to build the json but none of them were correct .I hop you understand my case clearly .
this is an example of my json structure:
{
"html":"the html code injected dynamical here"
}
as i said my main problem is that i am having problems caused by html symbols how can i solve it! please give me an explicit example on how to solve this problem and pars the html code successfully to json object.
I'm not sure about how you're building the JSON objects but if you're using json_encode() and json_decode() you shouldn't have any problems with HTML characters, they'll be escaped properly.
Not sure If I misread the question, but to pass json from server to client, use json_encode()
http://php.net/manual/en/function.json-encode.php
This should work for you and take care of all the escaping.
<?php
header('Content-type: application/json');
$json = array(
'html' => '<h1 style="color:#0F0">Hello World!</h1>'
);
echo json_encode($json);
exit;
Hmm, just learned this tidbit from reading the docs:
json_encode() won't work with
character sets other than UTF-8
I can't see why you'd need to send json to the server, maybe you can share the reason with us or someone can enlighten me in the comments?
Perhaps you could use url encoding.
You can encode the html. Some html encoding and decode can be find here:
http://code.google.com/p/jsool/source/browse/jsool-site/js/util/Encoder.js?r=176
You just need the html.encode and html.decode functions, that way you can do something like
function html_encode(string){
return string.replace(/./g,function(chr, index){
return chr.match(/[\w\d]/) ? chr : "&#"+chr.charCodeAt(0)+";" ;
});
},
function html_decode(string){
return string.replace(/&#[0-9]+;/g,function(text,index){
return String.fromCharCode(text.match(/[0-9]+/)[0]);
});
}
That way you can do
var html = html_encode(myhtml);
{"html": html}
to encode and recover the values using html_decode().
PS.: Sry, i didnt tested out the html_encode and decode functions, but its easy to find some at google.
If you are using jQuery this js library might help. http://www.rahulsingla.com/blog/2010/05/jquery-encode-decode-arbitrary-objects-to-and-from-json
In your javascript before you send the request do this:
var thing = {plugin: 'jquery-json', other: "blah-blah"};
var encoded = $.JSON.encode(thing);
Then after you send the request to php, do this to decode:
var thing = json_decode($_POST['thing']);
You can read more on this here
Due to the nature of my project. I am pulling data from my db and outputting to javascript. Things were working just fine till I got to the main content. It has strings like (;, :, - ''). How do I ensure that these are displayed without crushing my script coz as for now nothing seems to work.
If all you have is a single string value then see answer by Tomalak Geret'kal.
If there is any chance of getting something more than a single value from your database, like an array, object, null, or anything more complex, then I would suggest using json_encode. By using something like this:
<script>
var your_JavaScript_variable = <?php echo json_encode(your_PHP_variable); ?>;
</script>
you can pass complex data structures, arrays, or even single strings from PHP to JavaScript with all of your backslash escaping done automatically.
Additionally when you use JSON for moving your data from PHP to JavaScript it will be easy to make your application get the data from your server asynchronously without page refreshes using AJAX in the future.
You can use the PHP addslashes function for inserting into Javascript, and htmlspecialchars for inserting into HTML.
You should be encoding that data into json. PHP has a handy function to do this, json_encode.
Be sure to use the JSON_HEX_QUOTE option or the quotes in your data will break your js.
Read this: http://php.net/manual/en/function.json-encode.php
I have an HTML form POSTing to a PHP page.
I can read in the data using the $_POST variable on the PHP.
However, all the data seems to be escaped.
So, for example
a comma (,) = %2C
a colon (:) = %3a
a slash (/) = %2
so things like a simple URL of such as http://example.com get POSTed as http%3A%2F%2Fexample.com
Any ideas as to what is happening?
Actually you want urldecode. %xx is an URL encoding, not a html encoding. The real question is why are you getting these codes. PHP usually decodes the URL for you as it parses the request into the $_GET and $_REQUEST variables. POSTed forms should not be urlencoded. Can you show us some of the code generating the form? Maybe your form is being encoded on the way out for some reason.
See the warning on this page: http://us2.php.net/manual/en/function.urldecode.php
Here is a simple PHP loop to decode all POST vars
foreach($_POST as $key=>$value) {
$_POST[$key] = urldecode($value);
}
You can then access them as per normal, but properly decoded. I, however, would use a different array to store them, as I don't like to pollute the super globals (I believe they should always have the exact data in them as by PHP).
This shouldn't be happening, and though you can fix it by manually urldecode()ing, you will probably be hiding a basic bug elsewhere that might come round to bite you later.
Although when you POST a form using the default content-type ‘application/x-www-form-encoded’, the values inside it are URL-encoded (%xx), PHP undoes that for you when it makes values available in the $_POST[] array.
If you are still getting unwanted %xx sequences afterwards, there must be another layer of manual URL-encoding going on that shouldn't be there. You need to find where that is. If it's a hidden field, maybe the page that generates it is accidentally encoding it using urlencode() instead of htmlspecialchars(), or something? Putting some example code online might help us find out.