I am working on a light-weight inline content editor for my site. Similar to CushyCMS or SurrealCMS, the ‘editable’ elements of my page are defined with a class="editable".
I would like to pass 2 variables to php via AJAX:
$page The document to be written to (eg. ‘/path/index.html’)
$json A json string: {"0":"First Content","1":"Second Content","2":"Third Content"} where the key is the index of all elements with class="editable" and the value is each element’s innerHTML.
In PHP 5.3, how can I open $page and find/replace class="editable" elements?
$page = $_POST['page'];
$json = $_POST['json'];
$doc = new DOMDocument();
$doc = DOMDocument::loadHTMLFile($page);
// Detect DOM elements with class="editable" & replace content with JSON data
// Save changes to designated file
$doc->saveHTMLFile($page); // Write to file eg, 'index.html'
echo ('Success');
You can use XPath to track down all editable elements in the DOM.
Here it is again in the context of your original code: