AJAX 中的编码问题
我正在尝试在表单中创建两个下拉列表。我已经从 MySQL 数据库生成了第一个下拉列表。当我从第一个下拉列表中选择一个选项时,我必须通过从 MySQL 数据库中选择的第一个下拉列表的值生成第二个下拉列表选项。我通过使用此链接中的代码(AJAX)
http://www.w3schools.com/ 来完成此操作PHP/php_ajax_database.asp
我的问题是javascript调用的页面无法编码。此外,我使用的是阿拉伯语,因此第二个下拉菜单的结果是毫无意义的形状。
我尝试了不同的方法来解决问题,例如使用 header(..)、AJAX.get(..) 但没有人工作:(
我该如何解决这个问题?
提前致谢!! 问候, 马纳尔克
I'm trying to create two drop down list in a form. I have generated the first drop down list from MySQL database. When i select an option from first drop down list, i have to generate second drop down list options by the selected value of first drop down list from MySQL database. I did this by using the code from this link (AJAX)
http://www.w3schools.com/PHP/php_ajax_database.asp
My problem that the page called by javascript can't be encoded. Besides I'm using the Arabic lang so the outcome of the second drop down menu is meaningless shapes.
I have tried different ways to solve the problem like using header(..),AJAX.get(..)
but NO one works :(
how can i solve this problem?
Thanks in Advance!!
Regards,
Manalkk
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在后端尝试使用 utf8_encode()
on the back end try using utf8_encode()
一种方法是仅通过编码消息在客户端和服务器之间进行交换。如果请求和响应都包含特殊字符,那么您可以执行以下操作:
解码响应我经常使用 base64 编码来避免字符集问题,但您可以使用其他任何内容。
One way is to exchange between cliend and server by only encoded messages. If both request and response contains special chars then you can do:
I often use base64 encoding to avoid charset problems, but you can use anything else.
尝试将数据库连接设置为;
Try setting your database connection to;
浏览器会根据您在 http 标头中指定的字符集来解释它。这要求您发送的实际输出遵循该编码。
您的设置必须满足以下条件:
您必须在使用正确编码(SET NAMES“编码”)指定的连接上从 mysql 获取所有数据,默认为 latin1 / ISO-8859-1
PHP 内部仅适用于 latin1同样,但是任何动态数据都应该没问题,除非您不进行大量字符串操作,否则您可能会遇到问题。
如果 php-page 中的任何内容是静态的,则必须将 php-page 保存为相应的字符集(例如 utf8)。
php页面必须在标头中指定内容类型:
header("Content-Type: text/html;charset=utf8"); //替换为您正在使用的实际内容类型和字符集。
数据库中的数据必须从指定正确编码的连接进行相应存储,否则数据库将错误地转换为或从数据库中。
The browser till interpret it based on the character set that you specify in the http headers. That requires that the actual output that you are sending is following that encoding.
With your setup the following must be true:
You must fetch all data from mysql on a connection that is specified with the correct encoding (SET NAMES "encoding"), the default is latin1 / ISO-8859-1
PHP internally only work with latin1 as well, but any dynamic data should be okay unless you don't do a lot of string manipulation, then you might run into problem.
If any content in your php-page is static, you must save the php-page in the corresponding charset (for example, utf8).
The php page must specify content type in the header:
header("Content-Type: text/html;charset=utf8"); //replace with the actual content type and charset you are using.
Your data in the database must be stored accordingly, from a connection that has specified the correct encoding, otherwise the database will do incorrect conversion to or from the database.