6

I am using ajax request to send a value from client side to server side to insert in mysql database using ajax request. The value is in hindi language (हिन्दी मतलब जाने). But on client side when i alert it i get the hindi text as shown above but after the server side request is processed it gets inserted in the database as %u0939%u093F%u0928%u094D%u0926%u0940 %u092E%u0924%...

i have used in the client side

request1h[k].setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8;");
request1h[k].send(requestDatah); 

and on the server side header with php

<?php header("Content-Type: text/html; charset=utf-8");
mysqli_set_charset($con,"utf8");

then i read about utf8_encode and used that on the post value also

$value1 = utf8_decode($_POST['values']);

but still i can't insert the hindi text in the database as it is. Please help me what to do ?

miken32
  • 42,008
  • 16
  • 111
  • 154
  • in your server part when you dump `var_dump($_POST);` able to see the text in hindi...? – Tintu C Raju Nov 02 '15 at 04:29
  • This might help you http://stackoverflow.com/questions/11292898/how-to-insert-hindi-language-in-mysql – Disha V. Nov 02 '15 at 04:30
  • actually problem is only with the server side while using ajax.. if i dont use ajax i am able to use it properly without any error – Kushagra Singh Rajput Nov 02 '15 at 04:33
  • JavaScript strings are in 16 bit unicode, hence the `uxxxx` form. The `%` comes from the url-encoding triggered by `application/x-www-form-urlencoded` but PHP should be able to handle it. So give UTF-16 a try. – deamentiaemundi Nov 02 '15 at 04:39
  • @deamentiaemundi: its not working – Kushagra Singh Rajput Nov 02 '15 at 04:53
  • @SinghRajputKushagra ok. Or better: not ok. It could be UCS-2, too, but all Javascript engines innards I know do UTF-16, Did you chaneg teh DB, too? See e.g. here; https://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf16.html for further information but from what I know UTF-8 should work for the Hindi character set. Mmh...please try `$value1 = utf8_decode(urldecode($_POST['values']));` and check your database for the correct character-set settings. – deamentiaemundi Nov 02 '15 at 16:34
  • It won't be `UCS2` or `utf16`, even though `0939` is the ucs2 for `ह`, etc. AJAX will "url encode", so, you need to "url_decode" it before putting into a `CHARACTER SET utf8` MySQL column. – Rick James Dec 08 '15 at 01:41
  • Possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/q/279170/1255289) – miken32 Mar 23 '17 at 04:23

2 Answers2

0

for insert on your database you can try with these setup database with UTF8 AND COLLECTION UTF8 GENERAL

an for show language hindi you can put on your page HTML : the next:

<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />

for spetial character and other language you can use utf8

BlackHack123
  • 349
  • 1
  • 10
0

Use btoa function for Encode a string in base-64: It returns the value as a string, representing the strings encoded base-64.

By default, built-in functions in JavaScript, such as btoa and atob, do not support Unicode strings, as both functions treat all its characters as 16-bit-encoded strings. so you have to use first encodeURIComponent function.

base64_encode = function(str = "") {
  return btoa(unescape(encodeURIComponent(str)));
};

function log(message) {
  var client = new XMLHttpRequest();
  client.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      json = JSON.parse(this.response);
      console.log(json.form);
    }
  };
  client.open("POST", '//httpbin.org/post');
  client.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  client.send(message);
}
data = "key_name=" + base64_encode("हिन्दी मतलब जाने");
log(data);

If you need to decode that base64 to value in PHP file where you post data, use this:

// this will return 'हिन्दी मतलब जाने'
$value = base64_decode($_POST['key_name']);

And when you enter data into the database, make sure that the character set utf8_general_ci in your table must be set.

If this is your problem, you can convert the table using the following query:

ALTER TABLE `YOUR_TABLE_NAME` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
Bhavik Hirani
  • 1,996
  • 4
  • 28
  • 46