The Hindi text is appearing correctly on the webpage, however it was mistakenly inputted into the MySQL database

My current issue involves using an ajax request to send a value from the client-side to the server-side in order to update a MySQL database. The specific challenge arises when dealing with text in Hindi language (हिन्दी मतलब जाने). When I alert the text on the client-side, it displays correctly as shown above, but after processing the server-side request, it gets inserted into the database like this: à ¤¹à ¤¿à ¤Âनà ¥Ââ?¬ à ¤®à ¤¤... However, if I use JavaScript to alert request.responseText, it shows the correct text as हिन्दी मतलब जाने. Subsequently, upon reloading the page, the previously updated value appears incorrectly as ¤Â¹Ã ¤¿à ¤Âनà ¥Ââ?¬ à ¤®à ¤Âत..., while values that were not updated continue to display correctly as हिन्दी मतलब जाने.

In my client-side code, I have included:

var requestDatah = "values=" +
escape(valued) +"&texts=" +
encodeURIComponent(texted);
    request1h[k].setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8;");
    request1h[k].send(requestDatah);

And for the server-side handling, I'm setting the PHP header like this:

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

I am seeking assistance on how to rectify this issue and ensure proper storage and display of the Hindi text in the MySQL database.

Answer №1

Be sure to select the utf8 character set and utf8_general_ci collation.

It's important that the collation of the field where Hindi text will be stored is set to utf8_general_ci.

If you need to alter a table field, use this command:

 ALTER TABLE `<table_name>` CHANGE `<field_name>` `<field_name>`   VARCHAR(100) 
 CHARSET utf8 COLLATE utf8_general_ci DEFAULT '' NOT NULL;

Make sure to run this statement when connecting to the database:

mysql_set_charset('utf8');

For example:

 //set character set
 mysql_set_charset('utf8');

 //insert Hindi text
 mysql_query("INSERT INTO ....");

When retrieving data:

 //set character set
 mysql_set_charset('utf8');

//select Hindi text
 mysql_query("SELECT * FROM ....");

Before displaying any Unicode text (like Hindi) on a webpage, remember to set the content type with a meta tag:

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

For instance:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Example Unicode</title>
   </head>

     <body>
    <?php echo $hindiText; ?>
    </body>
   </html>

Update:

  The function mysql_query("SET CHARACTER SET utf8") has been replaced by mysql_set_charset('utf8'); 

This is now recommended way to change the charset. Avoid using mysql_query() for setting it (e.g. SET NAMES utf8). Refer to http://php.net/manual/en/function.mysql-set-charset.php*

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Sequencing asynchronous operations with node.js using sails.js

When setting up my Sails.js app, I have a series of tasks that need to be executed in order, with each task depending on the successful completion of the previous one. If any task encounters an error, the entire process should stop and prevent the app from ...

Creating a CLI TypeScript library capable of generating complete files similar to NestJS: A step-by-step guide

Lately, I've been diving into Nestjs and experimenting with creating my own custom libraries for the tasks I frequently work on. Despite my efforts, I'm still struggling to develop a library that functions seamlessly on the CLI and enables me to ...

Utilizing AJAX Requests in Google Extensions: Understanding the Basics and Maximizing Performance

I am in the process of creating a Google Chrome extension and everything seems to be going smoothly. However, I am looking for a way to use AJAX requests more freely, particularly within the same domain or within the file directory of the extension. For i ...

What is the best way to generate a cookie in svelte and retrieve it later on?

I have been working on implementing a cookie in Svelte (while also using Svelte Kit) for the purpose of storing a JWT Token used in authentication processes. I initially tried using pure JavaScript code like getCookie() and setCookie(), following the gui ...

What is the best way for a parent component to establish communication with a child component in a Vue.js application

Here is the existing code snippet: <div id='vnav-container'> <input type="text" v-model="searchTerm" v-on:keyup="search" class="vnav-input"> <menu :items="menu"></menu> </div> The main component features a ...

Select a specific date range from a form and export CSV file containing MySQL data within that range

I have successfully implemented a database CSV export functionality, but now I am looking to allow the user to specify a date range for the export process. Below is the HTML section that handles the date selection (date format: mm/dd/yyyy): <form act ...

Turn on Orientation in UIWebview during Javascript Evaluation

As I begin assessing the javascript, such as when a user selects text and it becomes highlighted, I encounter issues with the device orientation on my iPad. The orientation stops working until the javascript finishes executing. Is there a way to manage ja ...

The edge is showcasing a .jpg image in the form of a square randomly colored

I've encountered a new issue recently that seems to be related to a browser bug in Edge. I'm facing difficulties with .jpg images not displaying properly in Edge, although they show up fine in IE, Chrome, and Firefox. Instead of the images, all I ...

Error in Javascript: Attempted to save content of div element to text file, but encountered a TypeError as null is not recognized

I've been attempting to save the content of a div element into a text file but encountered the following error: TypeError: null is not an object (evaluating 'link.href = generateTxtFile('dummyText.innerText')') What seems to be ...

What is the best way to toggle between tabs using a checkbox that is checked or unchecked?

I have implemented a checkbox with two sets of tabs that are meant to be displayed or hidden based on the checked state of the checkbox. Currently, the checkbox switches tabs when checked, but does not switch back when unchecked. What modifications ca ...

Can a ng-click function be named as a string?

One approach I'm exploring involves returning the function as a string from JSON data. A simplified version of the JSON structure looks something like this: menu:[ {action:"setItem()", p1:"{{this}}", text:"Add Item"}, {action:"removeItem()", p1: ...

JavaScript loop not functioning properly to show modals when button is clicked

I successfully created a Javascript function to display a modal on button click, but I realized that I would have to manually repeat the code 56 times. Instead, I attempted to write a loop to automate this process, but unfortunately, the new function is no ...

Learn the process of integrating VueJS with RequireJS

I am attempting to set up VueJS with RequireJS. Currently, I am using the following VueJS library: . Below is my configuration file for require: require.config({ baseUrl : "js", paths : { jquery : "libs/jquery-3.2.1.min", fullcalendar : "libs/ful ...

Enhance your web form with the Bootstrap 4 tags input feature that allows users to add tags exclusively

I'm currently utilizing Bootstrap 4 along with Bootstrap Tags Input to create a multiple category selection feature. My goal is to enable users to choose multiple items exclusively from the predefined categories listed in the predefined_list. At pres ...

What is the best method for converting a variable with HTML content into a printable string?

In an attempt to display user-entered data from a text box to an HTML div, there seems to be an issue when the data contains HTML content. Instead of displaying the content as a string, it shows it as HTML elements. For example: let text = "<h1>Worl ...

Incorporate Voice Recognition into an HTML document

Specifically designed for mobile devices: I am looking to implement a feature on my mobile site where a button allows users to speak into their phones. After they finish talking, a random mp3 file will play. Would it be feasible to achieve this using JQue ...

Troubleshooting the issue of React not properly loading dynamically fetched HTML data using dangerouslySetInnerHTML

I am facing a challenge with dynamically loading HTML format data using dangerouslySetInnerHTML. Even though I am utilizing the axios plugin for AJAX API calls, the content is not being compiled and displayed as expected in the DOM. Strangely, when I hardc ...

Setting default values for route parameters in JavaScript

I'm looking to streamline my JavaScript code by simplifying it. It involves passing in 2 route parameters that are then multiplied together. My goal is to assign default values to the parameters if nothing is passed in, such as setting both firstnum ...

What is the process of instantiating a service constructor in Angular 2?

Below is the implementation of a service: import {Injectable} from 'angular2/core'; import {Service2} from './app.service2'; @Injectable() export class Service1 { constructor(service2:Service2) { this.service2 = service2; } ...

Enable seamless SCSS inclusion in Vue components through automatic importing

My goal is to import a variables.scss file globally in my Vue project. I have set up my vue.config.js file as follows: module.exports = { css: { loaderOptions: { scss: { additionalData: `@import "@/st ...