How to load javascript contents using php? - php

How to load javascript contents using php. Suppose when php saves a file using, file get contents and file put contents, JavaScript contents like Google current ads is never loaded but the google adsense codes are loaded.
Can php "run" the JavaScript code on a saved page in order to save the dynamic content that the code generates?
Already got the answer is NO.
Is there any way or no Way?

You are asking whether or not PHP can "run" the JavaScript code on a saved page in order to save the dynamic content that the code generates. The answer is no, it cannot. Or at least, not without building your own JavaScript interpreter.

Related

Output HTML and Download a File with PHP

I'm trying to create a 'download' page for my website. I'm using a url like this: http://example.com/download?file=21. The actual location of the file on the server is stored in a database along with the file ID. I would like the download page to display the name of the file that is being downloaded along with some other HTML. I'm having trouble figuring out how to both download a file and display output on the same PHP page.
All the advice I've seen for actually downloading a file with PHP requires changing the headers. Do I need to display the download page and then download the file with something like AJAX?
I would do it without ajax.
I would create a script to show the file with other html info http://example.com/file?id=21 and if the download param is set ( http://example.com/file?id=21&download=1 ) that script only prints the headers and sends the file.
Then, in http://example.com/file?id=21 I would create an empty iframe (in the code or dynamically with javascript) and then when a user wants to download the file you just have to set the url of that iframe to http://example.com/file?id=21&download=1 and the file will start to download. No need of ajax or reloading the website, only javascript to set the iframe url at the moment the user wants to download the file.
It's not the only way but I think it's a good one.

is it possible to pass a file uploaded using php to a javascript as input?

Im working on a tile based tmx editor and currently have file saving working. Currently it passes the tmx to a php file called endpoint that downloads the file. it can be viewed here www.jamesplanet.net/growtopiamapeditor/beta.
What I need to know is it possible to use php to upload a file then pass the file to javascript to load into the editor?
If you need a copy of the source I can put a link
If this question isn't worded correctly or hasn't enough information please don't down vote. instead let me know what else you need to know.
Basically this need to work , you have to use iframe , which you can create on the fly and use it as posted back to php code. This is one of the common way to achieve this. There will so many third party using this concept
Possible solution: You could have the PHP script download the file, insert the file details into a table and only return a name and/or id of how to get the file details from the table in JSON format for the javascript to add the file to the list.

PHP send data to parent window without using javascript

I am using FPDF to create a pdf document in an iFrame... During the pdf creation the script communicates and gets a lot of data from the server, and then I would like to display a progress bar.. That is why I have put the php generator in an iFrame.. then my plan was that the php script could send the looped data to the parent window..
e.g. every time a loop is made it says $count++;, then I know how many loops it has gone through, and I already know that it is going to limit the rows to the first 200 rows.. Then I would like to display the looped data in the parent widow like so: $count of §goal has been generated successfully!.. At the moment I am using jQuery, where I ask the php to echo some jQuery script every time a loop is made to display the results like so window.parent.count($count, $goal);.. Count in the parent winodw and it actually works well until the PDF has to be shown.. then I get an error message that tells me that the script is unable to display the PDF because the page already has outputted data..
Does anybody know how to make the PHP to send the data to the parent window, so I prevet the using of echo?
Sorry for my bad english.. if wished I can try to upload my script later for you to see...
I do it a bit differently...
I also wrote an application in which the PDF generator needs to fetch a rather large amount of data, so the generation takes a few seconds.
I use jQuery to fetch a php-page in the background. During this download, the screen turns gray and displays a classic "please wait"-circle. The only output that the php-file generates is an "OK" echo, together with a file link, when the generation has been completed. Instead of displaying the file inline, I save the PDF in a folder ($pdf->Output('filename.pdf','S')) and offer it as a download using the provided link and the jQuery-callback.
I hope you understand what I mean. Maybe this thought will help you a bit further.
EDIT: Don't know if this will work, but I just thought of it...
You could save the file and output the filename. Using jQuery, you could then refresh the contents of the iframe to fetch a page in which you display the already saved PDF inline...

Is it possible to change the content/mime type of ajax results?

I am using output buffering to grab a pdf file from a network share via PHP using a proxy-handler and readfile().
I display this in a new browser tab by specifying the content/mime type.
(http://stackoverflow.com/questions/10121903/php-scandir-explorer-view-to-display-network-share-files-file-via-wamp-wor)
I use jQuery / Ajax throughout the rest of the application.
Is it possible to inject the raw PDF content into a div tag using ajax but get it to display correctly? Currently the required header (header("Content-type: application/pdf")) seems to be stripped by the ajax request and it is displaying jibberish.
Edit: Thankyou all for the comments and answers. I will respond in more detail and review my options shortly.
# Murtaza - the question link above shows how I got to the raw PDF, in addition I have used $.ajax to send the request to the div tag and used an iframe which sort of worked when I rendered it into the code as src=, rather than Ajax.
# Brad Christie / Quentin - Perhaps I could use an iFrame or Modal to display the content? I have tested pdfobject.js which is good but requires a linked file (e.g. /files/file.pdf). It sounds horrific but what if I use PHP to write a temporary file to the web root and then display? The only other thing I can think could be useful might be the jQuery PDF library if anyone has used it?
# Ignacio - I did try this with the following code. It rendered the jibberish. I'm probably doing it wrong as I have no experience with iFrames.
// Where $filename is the raw PDF data
">
James.
You can't "inject the raw PDF content into a div tag"; the PDF file is not HTML and must go through a PDF reader plugin. Try making it the target of a iframe instead.
Is it possible to inject the raw PDF content into a div tag using ajax but get it to display correctly?
Not really. You might be able to make something of pdf.js though.
Currently the required header (header("Content-type: application/pdf")) seems to be stripped by the ajax request
Whatever means you are using to insert the data is explicitly treating it as text or HTML, not as a PDF. It is your your code is ignoring the content-type.

Get the content of a DIV and save to a file, jQuery/JS or Server-side

In my site I need to get content of one of DIV and save the content to a file.
I'm just wondering whether is there possibility to to do it in jQuery or JS if not do I have to use server based language like PHP.
Or any one got example or tutorial relative this query?
Using jQuery, access the content of the div using .text();
Then use jquery ajax function to POST this to a PHP script. Inside the php script you should open a file stream using fopen() with "w+" permission and then use fwrite() to write the contents into the file.
You can use jQuery's AJAX function to send the information to a script on the server that would create and save the file on the server.
Directly you cant use jQuery or JS to this purpose.
You could also use the HTML5 Filesystem API to save the file in the browser and use a[download] to present the user with a download link. Alternatively, you could skip the Filesystem and just do the latter.

Categories