开发者

read webpage (with dynamic content) and save

Im using file_get_contents() and the source is a url. Im also using ajax to insert html into the page.

The page when loaded:

<div>  
     <!-- Html goes here when the insert button is pressed -->

     <input type="button" onclick="insertHtml()" value="insert"/>
     <input type="button" onclick="saveThisPage()" value="save"/>
</div>

And when html is added dynamically with ajax:

<div>    
     <!-- Html goes here when the insert button is pressed -->
        <div>Some text here...</div>
        <div>Some text here...</div>
        <div>Some text here...</div>

     <input type="button" onclick="insertHtml()" value="insert"/>
     <input type="button" onclick="saveThisPage()" value="save"/>
</div>

Now I want to save what file_get_contents() returns and store the data into the mysql database.

I press the "save" button and a query is sent to a php file to read the page (url source) ...except what is 开发者_Python百科saved is the content when loaded WITHOUT the new data i.e

<div>Some text here...</div>

What do I do? What function(s) do I use?


It's still very difficult to understand what you are trying to do. But basing my answer on this comment:

@coosal: I forgot to add the "save" button in my question. But the process 1.Page Loads, 2.I press "insert and data is added and 3.I press "save" and page is saved. php is called AFTER ajax loads the data

You can do that. Simply implement a save button on the HTML page which triggers another AJAX request to a PHP script which then saves the completed page:

<button onClick="$.post('save.php', {content:$('body').html()})">

Then simply receive the complete html body via:

file_put_contents("/tmp/saved.html", $_POST["content"]);  // or mysql_*


It is not practical and even not possible to use file_get_contents() to retrieve the the whole content of the file alongwith the content of the file to be called via ajax because php already fetches the data before the javascript is being executed as javascript is totally client side script, i don't think it's possible.


When you load data using file_get_contents(), any Ajax code within the requested page will not be executed. You would need a browser to do that.


If you want the javascript to be executed, you should probably pull in the data through a browser component. On windows you may be able to use IE's IWebBrowser2 interface. On other platforms you can e.g. use gecko

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜