jQuery send HTML data through POST

后端 未结 4 1034
遥遥无期
遥遥无期 2020-12-03 03:13

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

相关标签:
4条回答
  • 2020-12-03 03:30

    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);

    0 讨论(0)
  • 2020-12-03 03:35

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

    0 讨论(0)
  • 2020-12-03 03:53

    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);
    
    0 讨论(0)
  • 2020-12-03 03:55

    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.

    0 讨论(0)
提交回复
热议问题