PHP echo and download pdf headers - php

Im in a situation where users can click a download button in a pop up. When they click "download", a request is made to my php script. The php script generates the PDF, echoes a script tag to close the modal and then downloads the PDF. I dont want to do a setTimeout to close the modal because of the timing differences for the PDF to be generated, which is why im echoing the script tag to close the modal. I've tried using ob_start/ob_end_flush but it still wont work. help! Thanks :)
The file gets downloaded, but the script tags wont get echoed. If I comment out the header & readfile, the script tags get echoed, so I know its just because output stops or something.
ob_start();
echo('<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>');
echo('<script>jQuery("body", window.parent.document).find("#pdfModal").addClass("out").removeClass("in")</script>');
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='results.pdf'");
readfile('../wp-content/plugins/ajaxpostfromfront/' . $token . '/results.pdf');
ob_end_flush();

Related

How to close php page from browser after executing the code?

I have a code in php which I want to schedule in task scheduler. But when this page runs it opens tab in my browser window. I want to close it anyway after execution or is there any way to run php page without opening tab in browser?
I have tried window.close(); but it's also not working.
You can use cURL. to run code in background or without opening browser tab.
see documentation cURL
It should be like:
filename.php
<?php
echo "I am here";
//here some php code
//adding close browser tab code
?>
<script>
window.close();
</script>
<?php
//here some php code
?>
Just run the script from your command line. There is no need to involve a browser at all.
http://php.net/manual/en/features.commandline.usage.php

PHP + generate .html file for download

I'm not sure what to even be searching for here, so I'll explain what I am attempting to do.
I current have a form (several dropdowns, input fields..etc) When the form is submitted, I am currently generating an 'output page' for print that has all user entered data on the initial form state.. with a 'print document' button at the bottom.
In which the end user would print out and write in any custom/specific comments..etc..
The above all works as intended.
However.. there has been a change in direction.. and instead of printing it out. (and hand writing any custom/unique comments by hand)..
...they want to be able to 'DOWNLOAD' the output/display page as a .html file
(so I'd need to make all assets in-file..etc. like styles, and probably based64 encode any/all images..etc)
As I have never done this before.. I'm not even surer what Im look for?
Should this be a separate (physical) .html file? (how do I get the submitted data/values into it before downloading?)
Should is just be a huge 'string' that I pull in the post values into?
But once that is complete.. how do I prompt the user to save it as a downloadable .html file?
Suppose you have your $html_string handling the contents of the page you want to download. It will be usefull to include CSS references.
<?php
$html_string.="<h1>This is your form</h1>";
foreach ($_POST as $p=>$value) $html_string.="{$p} has {$value}";
header('Content-Disposition: attachment; filename="readme.html"');
// other header() calls you need
die($html_string);
?>
This should work.
You could use "download" atribute in "a" html tag:
<a href="http://example.com/some.html" download>Download</a>
Unfortunately this feature is not supported by IE browser.

Html OnLoad Waiting

<?php
//stuff
?>
<html>
<head>...header stuff...</head>
<body onLoad="start_my_script ();">
.
.
. //get values from php.
.
.
</body>
</html>
My php is getting a little like 3 to 4 secs. to load stuff.
So, its not getting passed in html.
is there any way I can put wait in onload till my php gets loaded.
PHP runs when you request the page, not when the page is in the browser. Thus, PHP will process everything, make the final page, and send it to the browser. There is no chance for the browser to wait for the page to load before calling PHP.
i'd load it asynchronous (jQuery) to address those issues. However, it would help if you specify what are you doing in your php script and how those results are used in the html document.

Why would a.php file open as if it were an html file, rather than execute?

I have a .php file that I'm trying to execute. It's referred to in the 'action' part of an html form.
For some reason when the form submits it opens the .php file in the browser as if it were an html page (a blank one).
The .php file doesn't have anything out of the ordinary in it, but I'm not sure it's getting to the point of executing it anyway.
My opening form tag looks like this: <form action="my_script.php" method="post">
What am I missing?...
In all likelihood your script is executing. By default, HTTP headers will be sent indicating that the script's content is HTML, hence that's how your browser will treat it. But if you don't actually send any output, it'll appear as a blank page.
If you want the form to do something but not open a new page, maybe you could use AJAX to submit the form data without leaving the page. Alternatively, you could just add at the end of the script
echo 'Finished :)';
so that you know it has gotten to the end, and presumably done something.
When you submit a form, your browser posts data to the URL specified by the action attribute, as part of the request for that page.
Your page at my_script.php should output some HTML.
If instead you see your PHP code when you view the source of this new page, then PHP is not configured to be parsed on the server.
euh... php
Use: http://www.apachefriends.org/en/index.html instead of opening a html file directly in your browser.

How insert PHP output into html div replacing temp graphic

I need to display an html page and within the page is a div into which I want to echo the output of a PHP page.
While the PHP page is being fetched and prepared I want to display a "Loading" msg and a temporary gif.
I have the html code working
and I have the PHP code working/echo'ing.
How do I echo the contents of the PHP page to replace the original contents of the Div.?
(I guess - with an include:file and an innerhtml statement, but how/where?)
A Javascript solution would be fine.
Thanks
Add a div to your page with the image in it, then using jQuery:
$('#myDiv').load('mypage.php');
You're essentially looking to make an asynchoronous call to your php script once the page is loading, so JavaScript is your friend.
When the page loads, include the loading message and the temporary gif, then do an ajax call to your php script and insert the response into the relevant div.
As #Sjoerd points out - jQuery will do this for you nicely.
take a look into ajax:
The Low-down
Using ajax with jQuery

Categories