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
Related
i need to remove () backslash in my string when using echo json_encode()?
my example..
$song_url = 116e9155e0afc11555cf33dc9c9bd25d.mp3
$resmsg[] = array("Song_name"=>"$song_name","Song_URL"=>"http://www.kbmusique.com/songs/$song_url");
echo json_encode($resmsg);
my output is
[{"Song_name":"djigh araouioui","Song_URL":"http:\/\/www.kbmusique.com\/songs\/116e9155e0afc11555cf33dc9c9bd25d.mp3"}]
but i need as
[{"Song_name":"djigh araouioui","Song_URL":"http://www.kbmusique.com/songs/116e9155e0afc11555cf33dc9c9bd25d.mp3"}]
Is there a way to solve this? Thank you.
Your comment indicates that you just need to get a copy/pastable URL for testing.
Just parse the JSON and extract the piece of data you need from it. i.e. If you want a text representation of something, then convert the JSON to text, don't try to hack the JSON into a specific form.
You could do this in PHP with json_decode, in a browser with JSON.parse(), or just use a tool such as the Chrome JSONView extension.
i have this in JAVASCRIPT , a.php -
function gettemplate(realnam) {
alert(realnam)
}
i want to pass all the a[] array in func_a.php to the first file a.php.. to use the array there in javascript .
how i do that?
thanks a lot
EDIT--
ITS WORKS ! , if anyone need --
$a= json_encode($a);
echo "<SCRIPT LANGUAGE='javascript'> gettemplate('$a');</SCRIPT>\n";
:)
you can return (echo) in the func_a.php an json string http://de.php.net/manual/en/function.json-encode.php and parse it in javascript
echo json_encode($a);
You will need to serialize and parse the Array, as XMLHttpRequests only can contain XML or raw text. The format of choice is JSON, which is broadly supported, including PHP and JavaScript.
Serverside you will use json_encode. Don't forget to serve the JSON with a valid MIME type. You also should encode your error messages to be valid JSON.
Clientside, i.e. in the callback function, you will use JSON.parse on the xmlhttp.responseText.
You also will find lots of information about this on the web, you only need to search.
Read about JSON particulary json_encode. func_a.php must return something like this:
header('Content-type: application/json');
echo json_encode($a);
To get object in javascript use this:
var myResponseObject = JSON.parse(xmlhttp.responseText);
In another question, it was pointed out that using semantic markup can be a really clean way to pass data to an onclick function, along the lines of the code below.
I have a second question relating to passing JSON and having the receiving function recognize it as such. I have a PHP generated JSON value of [{"authed":"2012-03-04 17:24:24"},{"authed":"2012-03-04 11:44:38"}] that I need to pass to a function. echoing that straight into the <a> tag won't work, so I am using urlencode() to get:
click
Unfortunately, when I alert this out from popup(), I via the following code:
function popup(t) {
var auth = t.getAttribute('data-auth');
alert(decodeURI(auth));
}
I get
[{"authed"%3A"2012-03-04+17%3A24%3A24"}%2C{"authed"%3A"2012-03-04+11%3A44%3A38%22"}]
which JSON.parse() is unable to handle. Any suggestions on decoding/passing JSON using this pattern?
You need to use htmlspecialchars() to escape for html--like you should be doing for everything you echo out in html. (You are doing that right? To prevent generating invalid html?) Don't use urlencode(), which is for encoding parts of a url path or query parameter.
<?php
$data = array(array('authed'=>'2012-03-04 17:24:24'), array('authed'=>'2012-03-04 11:44:38'));
$jsondata = json_encode($data);
?>
<a data-auth="<?php echo htmlspecialchars($jsondata, ENT_COMPAT, 'UTF-8')?>" onclick="popup(this)">click</a>
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.
I've got a php script which generates HTML content. Is there a way to send back that HTML content through JSON to my webpage from the php script?
Yes, you can use json_encode to take your HTML string and escape it as necessary to be valid JSON (it'll also do things that are unnecessary, sadly, unless you use flags to prevent it). For instance, if your original string is:
<p class="special">content</p>
...json_encode will produce this:
"<p class=\"special\">content<\/p>"
You'll notice it has an unnecessary backslash before the / near the end. You can use the JSON_UNESCAPED_SLASHES flag to prevent the unnecessary backslashes. json_encode(theString, JSON_UNESCAPED_SLASHES); produces:
"<p class=\"special\">content</p>"
Do Like this
1st put all your HTML content to array, then do json_encode
$html_content="<p>hello this is sample text";
$json_array=array(
'content'=>50,
'html_content'=>$html_content
);
echo json_encode($json_array);
All string data must be UTF-8 encoded.
$out = array(
'render' => utf8_encode($renderOutput),
'text' => utf8_encode($textOutput)
);
$out = json_encode($out);
die($out);
In PHP:
$data = "<html>....";
exit(json_encode($data));
Then you should use AJAX to retrieve the data and do what you want with it. I suggest using JQuery: http://api.jquery.com/jQuery.getJSON/
You can send it as a String, why not. But you are probably missusing JSON here a bit since as far as I understand the point is to send just the data needed and wrap them into HTML on the client.
Just to expand on #T.J. Crowder's answer.
json_encode does well with simple html strings, in my experience however json_encode often becomes confused by, (or it becomes quite difficult to properly escape) longer complex nested html mixed with php. Two options to consider if you are in this position are: encoding/decoding the markup first with something like [base64_encode][1]/ decode (quite a bit of a performance hit), or (and perhaps preferably) be more selective in what you are passing via json, and generate the necessary markup on the client side instead.
All these answers didn't work for me.
But this one did:
json_encode($array, JSON_HEX_QUOT | JSON_HEX_TAG);
Thanks to this answer.