How to correctly handle PHP Ajax garbled _php Tutorial

Source: Internet
Author: User
Since XMLHTTP is using Unicode encoding to upload data, and the General page is gb2312, this results in garbled display of the page. When the page is fetched, XMLHTTP returns the Utf-8 encoding, which results in garbled display.

One of the workarounds for PHP Ajax is to display the declaration as GB2312 in a php file.

Header ("content-type:text/html;charset=gb2312");

The Chinese that is sent to the server is transcoded.

As follows

$_post["Content"]=iconv ("UTF-8", "gb2312", $_post["content"]);

So this can solve the PHP ajax garbled problem

Method Two, are all using UTF-8 encoding. There's not much to say here.

PHP Ajax garbled solution to the client of the sample

 
 
  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 title>
  6. < /head >
  7. < body>
  8. < div id="msg">< /div >
  9. < script language="javascript">
  10. /**
  11. * Initialize a 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. In the form test page, there is a form, a displayed layer
  31. function SendData ()
  32. {
  33. var msg = Document . getElementById ("msg");
  34. var F = Document . Form1;
  35. var C = F . Content.value;
  36. URL to receive 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=ten>
  53. < input type="button" value="OK"
    onclick = "SendData ()" > < !--I made a mistake when I used the submit-- >
  54. < /form >
  55. < /body >
  56. < /html >

PHP Ajax garbled solution to the server side of the example

 
  
  
  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[' contents ');
  7. }
  8. Else
  9. {
  10. Print ("No content sent");
  11. }
  12. ?>

The above code example is the PHP Ajax garbled related solutions, I hope that the need for a friend has helped.


http://www.bkjia.com/PHPjc/446195.html www.bkjia.com true http://www.bkjia.com/PHPjc/446195.html techarticle since XMLHTTP is using Unicode encoding to upload data, and the General page is gb2312, this results in garbled display of the page. The XMLHTTP returned when the page was fetched is the Utf-8 ...

  • 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.