I have a problem to get in a session array from php into a javascript, I tried to do it with json_encode, but I got problem with that since json not take swedish charatar and I need to have it in the array, any other suggestion how to do it!
I´m setting the sessions array here, the print_r is only for checking so it´s correct
while($row2[]=mysql_fetch_array($result2))
$_SESSION['row2'] = $row2;
print_r($_SESSION['row2'][0][0]);
print_r($_SESSION['row2'][0][1]);
print_r($_SESSION['row2'][0][2]);
and in my javascript i tried this
var row2 =<?php echo json_encode($_SESSION['row2']) ?>;
console.debug(row2);
Suggestion: Tell your database-client that you expect the data to be encoded as UTF-8 when you request/fetch it from the database. You do this by setting the client character encoding, see mysql_set_charsetDocs.
When you retrieve the data encoded as UTF-8 you can use it with json_encode and your website script's can properly receive it.
Related
Actually I want convert json array to string for that I used json_decode but it is returning nothing How to solve this?
Below is my array,
[{"Product":"Fantasy Brown","productimage":"images/fantasy-brown.jpg"},{"Product":"Bruno White","productimage":"images/bruno-white.jpg"},{"Product":"Barcunda Black","productimage":"images/barcunda-black.jpg"},{"Product":"Iceberg","productimage":"images/iceberg.jpg"},{"Product":"Mercury White","productimage":"images/mercury-white.jpg"},{"Product":"Desert Brown","productimage":"images/desert-brown.jpg"},{"Product":"Blue Venatino","productimage":"images/blue-venatino-marble.jpg"}]
the above array should be converted to string and also I want display product and productimage in string format from that array.
Below is my code,
$cart_items = "<script>document.write(localStorage.getItem('cart'));</script>";
echo "<pre>";
print_r($cart_items);
die;
$details = json_decode($cart_items);
// $x = $cart_items[];
echo $details;die;
You code wouldn't work. You're mixing client side and server side programming in a wrong way.
From the server point of view, $cart_items is only a string containing:
<script>document.write(localStorage.getItem('cart'));</script>
Nothing more.
It is your browser that parse the server output, i.e. the Javascript, to the JSON string. Since the conversion only happens on browser side, the server side (i.e. your PHP script) doesn't get it.
You need to reconsider your code logic. Maybe you need to have javascript that submit the localStorage content to server. Or maybe have your problem solved only with Javascript.
you all need to set json_decode() 'true' i.e
$details = json_decode($cart_items,true);
I have problem with saving utf-8 array in database with laravel.
when i save array in database it is stored like this
\u10e4\u10dd\u10dd\u10d3
when i display data it works fine, but when i run search query it displays nothing.
database encoding is utf-8 with general_ci collation.
i've already tried to encode data before saving in database
json_encode($data, JSON_UNESCAPED_UNICODE);
any ideas?
Try storing these arrays in your database using the PHP serialize function instead of encoding for JSON.
serialize($data);
When you want to use the data back, simply deserialize the array with PHP unserialize function.
unserialize($data);
Sending Unicode values from an html form (using JQuery ajax to PHP) results in question marks. The value is send using $.ajax and data = form.serialize(). The data looks like this before sending it to PHP %d9%86%D9%83 and so on. I tried to encode the value in the PHP side using many functions, but no luck.
I can provide a sample link if any one can help.
Thanks
Zib Nimer
Is your PHP file saved as Encode UTF-8?
Try using encodeURIComponent(fld.val()) and send it by itself and see what you get:- instead of serialize(), just to test.
I have a problem creating an input validation hash. JavaScript submits data to API and API validates the sent data with json_encode. Basically it works like this:
$input=array('name'='John Doe','city'=>'New York');
$validationHash=sha1(json_encode($input).$key); // Key is known to both servers
If PHP connects to another server, then everything works. It also works through JavaScript (I have a custom sha1() function there as well):
var validationHash=sha1(JSON.stringify({'name':'John Doe','city'=>'New York'})+key);
My problem comes when the string contains UTF-8 characters. For example, if one of the values is:
Ränisipelgasöösel
Then PHP server that receives the command converts it to this after JSON encoding:
R\u00e4nisipelgas\u00f6\u00f6sel
I need to do this in JavaScript as well, but I haven't been able to work out how. I need to make sure that I send proper validation hash to the server or the command fails. I found from Google that unescape(encodeURIComponent(string)) and decodeURIComponent() could be used, but neither gives me the same string that PHP has and validates with.
UTF-8 is used on both client and server.
Any ideas?
It does not seem to be possible. The only working solution I have found is to encode all data with encodeURIComponent() on browser side and with rawurlencode() on PHP side and then calculate the JSON from these values in arrays.
My fix was to raw url encode my json data like so.
rawurlencode( json_encode( $data ) );
And then from within javascript decode the raw url encoded json and then parse the json string like so.
JSON.parse( decodeURIComponent( data ) );
Hope this helps.
Why not base64 encode the data for safe transport? It encodes UTF-8 characters in a safe string that can be sent across different mediums - php, javascript etc. That way you can base64 decode the string at the receiving end. Voila!
By base64 encoding the data, i mean base64 encoding the values and not the whole json string
is you html page encoding utf-8?
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
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.