Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6719553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:06:23+00:00 2026-05-26T09:06:23+00:00

I am working on a light-weight inline content editor for my site. Similar to

  • 0

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');
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:06:24+00:00Added an answer on May 26, 2026 at 9:06 am

    You can use XPath to track down all editable elements in the DOM.

    $xpath = new DOMXPath($doc);
    $entries = $xpath->query('//*[@class="editable"]');
    $edits = json_decode($json, true);
    $num_edits = count($edits);
    
    for($i=0; $i<$num_edits; $i++)
    {
        $f = new DOMDocument();
        $edit = mb_convert_encoding($edits[$i], 'HTML-ENTITIES', "UTF-8"); 
        $f->loadHTML($edit);
        $node = $f->documentElement->firstChild;
        $entries->item($i)->nodeValue = "";
        foreach($node->childNodes as $child) {
            $entries->item($i)->appendChild($doc->importNode($child, true));
        }
    }
    

    Here it is again in the context of your original code:

    $page = $_POST['page'];
    $json = $_POST['json'];
    
    $doc = new DOMDocument();
    $doc = DOMDocument::loadHTMLFile($page);
    
    // Detect DOM elements with class="editable"  & replace content with JSON data
    $xpath = new DOMXPath($doc);
    $entries = $xpath->query('//*[@class="editable"]');
    $edits = json_decode($json, true);
    $num_edits = count($edits);
    
    for($i=0; $i<$num_edits; $i++)
    {
        $f = new DOMDocument();
        $edit = mb_convert_encoding($edits[$i], 'HTML-ENTITIES', "UTF-8"); 
        $f->loadHTML($edit);
        $node = $f->documentElement->firstChild;
        $entries->item($i)->nodeValue = "";
        foreach($node->childNodes as $child) {
            $entries->item($i)->appendChild($doc->importNode($child, true));
        }
    }
    
    // Save changes to designated file
    $doc->saveHTMLFile($page); // Write to file eg, 'index.html'
    
    echo ('Success');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on udhcpd , a light weight dhcp server that comes with
Im working on a fairly light weight client application (desktop app in .Net as
I'm looking for a light-weight UI toolkit written in Javascript (something like Swing) but
The android app I am working on overrides the Application class to store lightweight
So I am working on the landing page for one of my little products.
I'm working on a simple HTML site and having trouble with the navigation not
It is about the efficiency, which chrome being light weight, fine for general web
I am trying out NicEdit witch is super light weight and easy to install.
I am trying to create a light weight web interface using embedded jetty to
I'm working on a project that needs a simple, lightweight event server (i.e. a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.