Home >Backend Development >PHP Tutorial >PHP---ajax passes the POST value (including html tag) to the submission page and the data is lost or garbled
Question
?? By getting the content in the text editor (an html text), and then using ajax to pass the text content to the background through POST, save it as a xxx.html file, but opening this file in the browser will Data loss or garbled characters occur.
Reason
?? This is because the text contains html tags. When using POST to transfer the value, some characters such as '&' in the value content will affect the extraction of the variable value, resulting in garbled characters or data loss. .
Solution
?? Encode the html text content to be passed and then pass it over. Then use the browser's automatic decoding.
Encoding function
??encodeURIComponent() function can encode strings as URI components. This method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation characters: - _ . ! ~ * ’ ( ) .
Other characters (such as ;/?:@&=+$,# which are punctuation marks used to separate URI components) are replaced by one or more hexadecimal escape sequences. If the URI component contains delimiters, such as ? and #, you should use the encodeURIComponent() method to encode each component separately, and otherwise you can use the encodeURI(URIstring) function to encode.
<code><span>var</span> content = UM.getEditor(<span>'myEditor'</span>).getAllHtml(); <span>/*对待传递的值的内容进行编码*/</span><span>var</span> htmlcontent = <span>encodeURIComponent</span>(content); alert(htmlcontent); <span>var</span> postStr = <span>'news_title='</span> + news_title +<span>'&htmlc/span> + htmlcontent + <span>'&sid='</span>+<span>Math</span>.random(); alert(postStr); ajax(<span>"../news_submit_hand.php"</span>,postStr,<span><span>function</span><span>(result)</span>{</span><span>if</span>(result == <span>'submit_success'</span>){ alert(<span>"新闻提交成功!请通过右上角的关闭按钮来关闭编辑器。"</span>); } <span>else</span> { alert(<span>"新闻提交出错!"</span>); } });</span></code>
Problems that still need to be solved after encoding
?? A title is passed in during the news submission. To insert the title before the body of the html text content, you need to insert the title after the mark. At this time, it becomes %3Cbody%3E after encoding. There are solutions:
??First split the original string into two strings from the specified position through the %3Cbody%3E string, and then combine the first half of the split string + the string to be inserted + the second half of the string to form a new String, thus realizing the function;
??Code implementation:
<code><span><span>function</span><span>str_insert</span><span>(<span>$str</span>, <span>$i</span>, <span>$substr</span>)</span> {</span><span>for</span>(<span>$j</span>=<span>0</span>; <span>$j</span><<span>$i</span>; <span>$j</span>++){ <span>$startstr</span> .= <span>$str</span>[<span>$j</span>]; } <span>for</span> (<span>$j</span>; <span>$j</span><strlen(<span>$str</span>); <span>$j</span>++){ <span>$laststr</span> .= <span>$str</span>[<span>$j</span>]; } <span>$str</span> = (<span>$startstr</span> . <span>$substr</span> . <span>$laststr</span>); <span>return</span><span>$str</span>; } <span>$myfile</span> = fopen(<span>$news_path</span>, <span>"w"</span>); <span>if</span>(<span>'FALSE'</span> == <span>$myfile</span>)<span>echo</span><span>'error'</span>; <span>/*处理新闻标题等信息*/</span><span>//待插入的html代码</span><span>$insert_html</span> = <span>"<div><h3 style=\"text-align:center;\">". <span>$news_title</span> . <span>"</h3><div style=\"text-align:center;width:100%;font-size:8px;color:#A9A9A9;\"><span>发布者:admin</span> <span>发布日期:"</span>. date(<span>"Y-m-d H:i:s"</span>,<span>$timestamp</span>) .<span>"</span></div></div><hr style=\"border:1px dashed #000; height:1px\">"</span>; <span>$first_pos</span> = stripos(<span>$htmlcontent</span>, <span>"%3Cbody%20%3E"</span>); <span>$first_pos</span> += <span>13</span>; <span>$content</span> =str_insert(<span>$htmlcontent</span>,<span>$first_pos</span>,<span>$insert_html</span>); fwrite(<span>$myfile</span>, <span>$content</span>); fclose(<span>$myfile</span>);</code>
Copyright statement: This article is an [original] article by the blogger. It may be reproduced without the blogger’s permission. Please indicate the source of the blog: [http://blog.csdn.net/FreeApe]
The above introduces how PHP---ajax transfers POST values (including html tags) to the submission page where the data is lost or garbled, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.