简体   繁体   English

使用JQuery或JavaScript自动提交

[英]auto submit with JQuery or JavaScript

I need to send a var which contains html code. 我需要发送一个包含html代码的var。

I have a file with html code, and I read it and put that HTML code in a var with 我有一个包含html代码的文件,我阅读了该文件,并将该HTML代码放入var

ob_start();
$output = ob_get_contents();
ob_end_clean();

I tried to send it via GET with header(location:); 我试图通过带有header(location :);的GET发送它; but I get the error (headers already sent). 但我收到错误消息(标题已发送)。 so I want to send it via JQuery or JavaScript. 所以我想通过JQuery或JavaScript发送它。

I tried to do this 我试图做到这一点

var output = $('#out').text(); 
        $.post('../tcpdf/examples/polizaPDF.php',{output:output})
    .done(function(data) 
    {
        console.log($.trim(data));
    });

But I need to stay in the page "polizaPDF.php". 但是我需要保留在“ polizaPDF.php”页面中。

What do I do in polizaPDF.php? 我在polizaPDF.php中做什么? I print the var output which contains the html code. 我打印包含html代码的var输出。 So I can get a PDF in the browser. 因此,我可以在浏览器中获取PDF。 I use tcpdf to print the pdf. 我使用tcpdf打印pdf。

Can anyone help me or give me some advice to do this? 谁能帮助我或给我一些建议呢?

You might want to check the documentation on Ajax calls. 您可能想查看有关Ajax调用的文档。 Here's an example on how to pass data to a file : 这是有关如何将数据传递到文件的示例:

$.ajax({
    type: "GET",
    url: "../tcpdf/examples/polizaPDF.php",
    data: { output: output }
})
.done(function(data) {
    console.log($.trim(data));
});

Then on your polizaPDF.php you now have access to the php variable $_GET['output'] 然后在polizaPDF.php上,您现在可以访问php变量$_GET['output']

If you do POST with JQUERY AJAX, then it will send it to polizaPDF.php, and that script processes it. 如果使用JQUERY AJAX进行POST,则它将发送到polizaPDF.php,然后该脚本对其进行处理。

I think by sending request through AJAX POST you will not receive PDF output. 我认为通过AJAX POST发送请求不会收到PDF输出。 Because to receive PDF file output in browser, your PHP Script should send fresh HTTP Headers, and also you have to mention PDF in header. 因为要在浏览器中接收PDF文件输出,所以您的PHP脚本应该发送新的HTTP标头,并且您还必须在标头中提及PDF。

You have to use this code to send PDF file to browser, and this needs fresh header. 您必须使用此代码将PDF文件发送到浏览器,这需要新的标头。

header("Content-type:application/pdf"); 报头( “内容类型:application / PDF”);

Happy Coding Atul Jindal 快乐编码Atul Jindal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM