| |

VerySource

 Forgot password?
 Register
Search
View: 2741|Reply: 10

ajax, php garbled problem

[Copy link]

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-2-13 10:00:01
| Show all posts |Read mode
I made a small example, only submit the message content, why is there garbled characters? ?
The one who knew how to solve it, searched a lot, and it didn't work.

ajax.php

<! 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 = gb2312" />
<title> Untitled document </ title>
<script language = "javascript">
function InitAjax ()
{
Var ajax = false;
Try {
Ajax = new ActiveXObject ("Msxml2.XMLHTTP");
} Catch (e) {
Try {
Ajax = new ActiveXObject ("Microsoft.XMLHTTP");
} Catch (E) {
Ajax = false;
}
}
If (! Ajax&&typeof XMLHttpRequest! = 'Undefined') {
Ajax = new XMLHttpRequest ();
}
Return ajax;
}

function saveUserInfo ()
{
// Get the return information layer
Var msg = document.getElementById ("msg");
/// Get form object and user information values
Var f = document.form1;
Var name = f.name.value;
Var content = f.content.value;
/// Receive form URL
Var url = "save.php";
// Need the value of POST, connect each variable through&
Var postStr = "name =" + name + "&content =" + content;
// Instantiate Ajax
Var ajax = InitAjax ();

// Open the connection through Post
Ajax.open ("POST", url, true);
/// Define the HTTP header information of the transmitted file
Ajax.setRequestHeader ("Content-Type", "application / x-www-form-urlencoded; charset = gb2312");
// Send POST data

ajax.send (postStr);
// Get execution status
Ajax.onreadystatechange = function () {
// If the execution status is successful, then write the returned information to the specified layer
If (ajax.readyState == 4&&ajax.status == 200) {
Msg.innerHTML = ajax.responseText;
}
}
}

</ script>
</ head>

<body>
<form id = "form1" name = "form1">
<div id = msg> </ div>
  <label>
  <div align = "center">
    <input name = "name" type = "text" id = "name" />
  </ div>
  </ label>
  <p align = "center">
    <textarea name = "content" rows = "8" id = "content">
Reply

Use magic Report

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-10 21:45:02
| Show all posts
Submit in English,
Chinese character error Data too long for column 'name' at row 1
I have added mysql_query ("SET NAMES 'GB2312'");
If the SQL is directly changed to $ sql = "insert into gbook (name, content) values ​​('you you you you you', 'you you you')";
Passing Chinese characters from ajax.php will not work.

why? ? ?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 United States

Post time: 2020-4-13 15:15:01
| Show all posts
Convert the title and content iconv to try encoding
Reply

Use magic Report

0

Threads

27

Posts

18.00

Credits

Newbie

Rank: 1

Credits
18.00

 China

Post time: 2020-4-15 20:15:02
| Show all posts
$ objResponse = new xajaxResponse ('gb2312');

See if this is possible.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-4-17 09:15:02
| Show all posts
If your database is encoded in Latin, char (1) represents a single English character.To use this encoding to store Chinese, it takes two bytes.
If the provincial code of your database is GB2312 or utf8, char (1) means one Chinese character and two bytes.
Also, JavaScript itself is encoded in UTF8. It is recommended that your php file is encoded in UTF8.
Reply

Use magic Report

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-17 16:15:01
| Show all posts
What if gb2312 is used? ? The entire website is mostly used gb2312
I also added this sentence ajax.setRequestHeader ("Content-Type", "application / x-www-form-urlencoded; charset = gb2312");
Reply

Use magic Report

0

Threads

27

Posts

18.00

Credits

Newbie

Rank: 1

Credits
18.00

 China

Post time: 2020-4-18 17:45:01
| Show all posts
Transcode of obtained data, iconv
Reply

Use magic Report

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-19 14:00:01
| Show all posts
Iconv also tried, the problem remains
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-4-19 22:30:01
| Show all posts
The data you sent in the past is utf-8 encoded ...
$ con = iconv ("utf-8", "gbk", $ con);
$ name = iconv ("utf-8", "gbk", $ name);

Then insert it into the database, then there is no problem. The encoding of the data sent by ajax has nothing to do with the page encoding, because the browser uses unicode to process the data ...
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-4-19 23:15:01
| Show all posts
Iconv also tried, the problem remains
?

impossible...

However, you have a problem with the javascript that sends the data.You should use encodeURIComponent to encode the string before sending it to the server.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list