0

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 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?

Zebra
  • 3,858
  • 9
  • 39
  • 52
  • You can't do that with file_get_contents. – Alec Gorge Feb 13 '11 at 18:38
  • @alecgorge: how do I do this then? – Zebra Feb 13 '11 at 18:39
  • Well you can use some form of browser remoting, if you want to read the html after javascript was executed. Not sure why @alecgorge is being secretive with you, but see here for one possible implementation http://stackoverflow.com/questions/3836095/control-virtual-web-browser-in-any-language – mario Feb 13 '11 at 18:44
  • of course you haver permission from the site owner to do this –  Feb 13 '11 at 19:08

4 Answers4

2

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_*
mario
  • 144,265
  • 20
  • 237
  • 291
  • Does this append the html body to the query. Like this: save.php?content="the whole page" -if this is the case, is this ok to do? – Zebra Feb 13 '11 at 19:35
  • @dany: That's about what it should do. Albeit it sends the data via POST, not GET parameters. – mario Feb 13 '11 at 20:00
1

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.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Im not loading with file_get_contents, Im reading with file_get_contents. however I dont get the new data. – Zebra Feb 13 '11 at 18:42
  • @dny I don't understand what the difference is between "Loading" and "reading". Can you show some code? Anyway, PHP is not equipped to interpret JavaScript. You will need a browser for that. – Pekka Feb 13 '11 at 18:43
  • 1
    @Saul please quit your revenge downvoting. It's childish and petty. – Pekka Feb 13 '11 at 18:58
1

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.

kushalbhaktajoshi
  • 4,640
  • 3
  • 22
  • 37
  • 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 – Zebra Feb 13 '11 at 19:10
0

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

Jaap Versteegh
  • 761
  • 7
  • 15