Search code examples
phpjquerysimple-html-dom

manipulate div content using simple_html_dom


test01.php

<div id="res"></div>

I need to change the content of #res on server side.

ajax.php

include ("simple_html_dom.php");  // the same folder as `ajax.php`
$url = $_POST["url"];  // http://localhost/up/matria/test01.php - that's ok
$html = file_get_html($url);
$html->find('div#res')->innertext = '525';

To save changes I tried:

$html->save($url);  // first try
file_put_contents($url, $html);  // next try
$htmlString = $html->save();
file_put_contents($url);  // next try

Reloading the page - res is still empty.


Solution

  • After you change the innertext, go ahead and save the html back to the file

    Like:

    $html->save(path to file on disk);
    

    Alternatively you can get the html string back and save it using PHP file system functions:

    $htmlString = $html->save();
    file_put_contents(path to file on disk);