jQuery的通过POST发送的HTML数据 [英] jQuery send HTML data through POST

查看:154
本文介绍了jQuery的通过POST发送的HTML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery做一个POST到一个div的HTML内容的PHP文件。 HTML内容包含表,输入较小的div我想抓住主DIV的内容,并将其发送到数据库。唯一的选择我能想到的是POST方法,但我不知道我是否可以发送纯HTML它。是否有任何其他选项从一个div发送HTML内容的PHP文件插入到MySQL?

I am using jQuery to make a POST to a PHP file with the HTML content of a div. The HTML content contain tables, inputs, smaller divs and I would like to grab the content of the main DIV and send it to the database. The only option I could think of is the POST method but I don't know if I can send plain HTML with it. Are there any other options for sending HTML content from a div to a PHP file to be inserted into MySQL ?

感谢。

编辑:我现在可以发送完整的HTML数据与jQuery的POST。然而,从DIV我的HTML字符被转换成特殊字符。例如:>中将成为&放大器; GT;而当这种情况发生时,我的POST数据仅限于其中第一个特殊字符出现,因为我做我的AJAX POST是这样的:

I am now able to send full HTML data with jQuery's POST. However, my HTML characters from that DIV gets transformed into special characters. Example: ">" would become ">" and when that happens, my POST data is limited to where the first special character appears because I do my ajax POST like this:

var data = 'id='+ currid +'&html='+ div_html;

    $.ajax({
          type: "POST",
          url: "file.php",
          data: data,
    .......................
    .......................

使用这个code也不行,因为div_html包含其他&放大器;字符,以便将它视为另一篇文章中的参数发送。

Using this code is not ok because div_html contains other "&" characters so it would be considered as another POST parameter to be sent.

任何变通办法?

再次感谢。

推荐答案

至于你担心,一旦你拉出来的内容与类似的 HTML()它只是一个字符串。您可以测试与

As far as you're concerned once you've "pulled out" the contents with something like .html() it's just a string. You can test that with

<html> 
  <head>
    <title>runthis</title>
    <script type="text/javascript" language="javascript" src="jquery-1.3.2.js"></script>
    <script type="text/javascript">
    	$(document).ready( function() {
    		var x = $("#foo").html();
    		alert( typeof(x) );
    	});
    </script>
    </head>
    <body>
    	<div id="foo"><table><tr><td>x</td></tr></table><span>xyz</span></div>
    </body>
</html>

提醒文本是字符串。只要你不把它传递给一个解析器有没有神奇的它,它像任何其他字符串的字符串。
没有什么妨碍您使用。员额()这个字符串发送回服务器。

The alert text is string. As long as you don't pass it to a parser there's no magic about it, it's a string like any other string.
There's nothing that hinders you from using .post() to send this string back to the server.

修改:不要传递一个字符串作为参数的数据,用以。员额(),但一个对象,像

edit: Don't pass a string as the parameter data to .post() but an object, like

var data = {
  id: currid,
  html: div_html
};
$.post("http://...", data, ...);

jQuery将处理参数的编码。
如果(无论出于何种原因)想保持你的字符串,你必须连接code的东西,如逃生的值()

jquery will handle the encoding of the parameters.
If you (for whatever reason) want to keep your string you have to encode the values with something like escape().

var data = 'id='+ escape(currid) +'&html='+ escape(div_html);

这篇关于jQuery的通过POST发送的HTML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆