jQuery send HTML data through POST

2019-01-11 06:27发布

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 ?

Thanks.

EDIT: 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,
    .......................
    .......................

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

Any workarounds ?

Thanks again.

4条回答
做自己的国王
2楼-- · 2019-01-11 07:03

jQuery.post(post_url,{ content: "John" } )
	.done(function( data ) {
		 
		 
	});
 

I used the technique what u have replied above, it works fine but my problem is i need to generate a pdf conent using john as text . I have been able to echo the passed data. but getting empty in when generating pdf uisng below content ples check

ob_start();
			 			 
			include_once(JPATH_SITE .'/components/com_gaevents/pdfgenerator.php');
			$content = ob_get_clean();
			
		
		
			 $test = $_SESSION['content'] ;
	 
			require_once(JPATH_SITE.'/html2pdf/html2pdf.class.php');
            $html2pdf = new HTML2PDF('P', 'A4', 'en', true, 'UTF-8',0 );			
				$html2pdf->setDefaultFont('Arial');
				$html2pdf->WriteHTML($test);

查看更多
\"骚年 ilove
3楼-- · 2019-01-11 07:04

If you want to send an arbitrary amount of data to your server, POST is the only reliable method to do that. GET would also be possible but clients and servers allow just a limited URL length (something like 2048 characters).

查看更多
倾城 Initia
4楼-- · 2019-01-11 07:09

I don't see why you shouldn't be able to send html content via a post.

if you encounter any issues, you could perhaps use some kind of encoding / decoding - but I don't see that you will.

查看更多
甜甜的少女心
5楼-- · 2019-01-11 07:14

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 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);
查看更多
登录 后发表回答