PHP --- ajax transmits the POST value (including html tags) to the submitted page. data is lost or garbled.

Source: Internet
Author: User
: This article mainly introduces the data loss or garbled characters when PHP --- ajax transmits POST values (including html tags) to the submitted page. For more information about PHP tutorials, see. Problem

?? But when you open the file in the browser, data loss or garbled characters may occur.

Cause

?? This is because the text contains html tags. when using POST to pass values, some characters such as '&' in the value content will affect the extraction of variable values, cause garbled characters or data loss.

Solution

?? Encode the html text to be passed, and then pass it over. Then you can use the browser to automatically decode it.

Encoding functions

?? The encodeURIComponent () function can encode a string as a URI component. This method does not encode ASCII letters and numbers, and does not encode these ASCII punctuation marks :-_.! ~ *'().
Other characters (such :;/? : @ & =+ $, # The punctuation marks used to separate URI components) are all replaced by one or more hexadecimal escape sequences. If the URI component contains a separator, such? And #, the encodeURIComponent () method should be used to encode each component, and the other can be encoded using the encodeURI (URIstring) function.

Var content = UM. getEditor ('myeditor '). getAllHtml ();/* encode the content of the passed value */var htmlcontent = encodeURIComponent (content); alert (htmlcontent ); var postStr = 'news _ title = '+ news_title +' & htmlc/span> + htmlcontent + '& sid =' + Math. random (); alert (postStr); ajax (".. /news_submit_hand.php ", postStr, function (result) {if (result = 'submit _ success ') {alert (" News submitted successfully! Close the editor using the close button in the upper right corner. ");} Else {alert (" An error occurred while submitting the news! ");}});

Problems to be solved after encoding

?? A title is included in the News Submission. to insert the title to the body of the html text, you need to insert the title after the mark. The encoding is changed to % 3 Cbody % 3E. There are solutions:
?? First, use the % 3 Cbody % 3E string to split the original string into two strings at the specified position, and then split the first half of the string + the string to be inserted + the second half of the string, form a new string to implement the function;
?? Code implementation:

Functionstr_insert ($ str, $ I, $ substr) {for ($ j = 0; $ j <$ I; $ j ++) {$ startstr. = $ str [$ j];} for ($ j; $ j
  
   
Publisher: admin Release date: ". date (" Y-m-d H: I: s ", $ timestamp )."
   

"; $ First_pos = stripos ($ htmlcontent," % 3 Cbody % 20% 3E "); $ first_pos + = 13; $ content = str_insert ($ htmlcontent, $ first_pos, $ insert_html); fwrite ($ myfile, $ content); fclose ($ myfile );

Copyright Disclaimer: This article is an article [original] by the blogger. it can be reproduced without the permission of the blogger. it indicates the source of the blog: [http://blog.csdn.net/FreeApe]

The above introduces PHP --- ajax transmission of POST values (including html tags) to the submission page data loss or garbled characters, including the content, hope to be helpful to friends interested in PHP tutorials.

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.