How to correctly handle PHPAjax garbled code _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
How to correctly handle PHPAjax garbled characters. Because XMLHTTP uses Unicode encoding to upload data, and generally uses gb2312 as the page, garbled characters are generated when the page is displayed. When retrieving the page, XMLHttp returns UTF-8 encoding.Because XMLHTTP uses Unicode encoding to upload data, and generally uses gb2312 as the page, garbled characters are generated when the page is displayed. When the page is obtained, XMLHttp returns UTF-8 encoding, which causes garbled characters in the display.

One of the solutions to PHP Ajax garbled code is to display the declaration as GB2312 in the PHP file.

Header ("Content-Type: text/html; charset = GB2312 ");

Transcode the Chinese characters sent to the server.

As follows:

$ _ POST ["content"] = iconv ("UTF-8", "gb2312", $ _ POST ["content"]);

This can solve the PHP Ajax garbled problem.

Method 2, is using UTF-8 encoding. I will not talk about it here.

PHP Ajax garbled solution example client

 
 
  1. <! Doctype html public "-// W3C //
    Dtd html 4.01 Transitional // EN ">
  2. <Html>
  3. <Head>
  4. <Meta http-equiv = "Content-Type"
    Content = "text/html; charset = gb2312">
  5. <Title> ajax post test
  6. </Head>
  7. <Body>
  8. <P id = "msg"> </p>
  9. <Script language = "javascript">
  10. /**
  11. * Initialize an xmlhttp object
  12. */
  13. Function InitAjax ()
  14. {
  15. Var ajax = false;
  16. Try {
  17. Ajax = new ActiveXObject
    ("Msxml2.XMLHTTP ");
  18. } Catch (e ){
  19. Try {
  20. Ajax = new ActiveXObject
    ("Microsoft. XMLHTTP ");
  21. } Catch (E ){
  22. Ajax = false;
  23. }
  24. }
  25. If (! Ajax & typeof XMLHttp
    Request! = 'Undefined '){
  26. Ajax = new XMLHttpRequest ();
  27. }
  28. Return ajax;
  29. }
  30. // There is a form and a display layer on the form Test page.
  31. Function sendData ()
  32. {
  33. Var msg = document. getElementById ("msg ");
  34. Var f = document. form1;
  35. Var c = f. content. value;
  36. // The URL for receiving data
  37. Var url = "dispmsg. php ";
  38. Var poststr = "content =" + c;
  39. Var ajax = InitAjax ();
  40. Ajax. open ("POST", url, true );
  41. Ajax. setRequestHeader ("Content-Type ",
    "Application/x-www-form-urlencoded ");
  42. Ajax. send (poststr );
  43. Ajax. onreadystatechange = function (){
  44. If (ajax. readyState = 4 & ajax. status = 200 ){
  45. Alert ("I got something ");
  46. Msg. innerHTML = ajax. responseText;
  47. }
  48. }
  49. }
  50. </Script>
  51. <Form name = 'form1'>
  52. <Input type = "text" name = 'content' size = 10>
  53. Onclick = "sendData ()"> <! -- An error occurred when I used the submit -->
  54. </Form>
  55. </Body>
  56. </Html>

PHP Ajax garbled solution example on the server side

 
 
  1. <? Php
  2. Header ("Content-Type: text
    /Html; charset = GB2312 ");
  3. If ($ _ POST ['content'])
  4. {
  5. $ _ POST ["content"] = iconv ("
    UTF-8 "," gb2312 ", $ _ POST [" content "]);
  6. Print ("content is". $ _ POST ['content']);
  7. }
  8. Else
  9. {
  10. Print ("no content to send ");
  11. }
  12. ?>

The above code example is a solution to PHP Ajax garbled code. I hope it will be helpful to other users who need it.


Bytes. When retrieving the page, XMLHttp returns UTF-8 encoding...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.