0

PHP. How to place an element between specific tags?

Hi all! I really need your help! How to place an element between specific tags? my code.. unfortunately it doesn't work, no data is saved $filename = $_COOKIE['user'].'1234.txt'; $old_list = file_get_contents($filename); $temp_list = new DOMDocument(); $temp_list->loadHTML($old_list); $sector = $temp_list->getElementById('first_list'); $fragment = $temp_list->createDocumentFragment(); $fragment->appendXML('<div>Hello, world!</div>'); $sector->appendChild($fragment); $temp_list->saveHTML(); file_put_contents($filename, $temp_list); what could be the error? I'll be glad for any help!

22nd Feb 2024, 5:18 PM
W G
W G - avatar
5 Respostas
+ 1
I don't like asking any AI for help, but it wrote this: It seems like you're attempting to insert a <div>Hello, world!</div> element into an existing <div> element with the ID 'first_list'. Your code looks mostly correct, but there are a few things to note: 1. Make sure that the element you're trying to target with the ID 'first_list' exists in your HTML file. 2. After making modifications to the DOM, you need to capture the modified HTML content and save it back to the file. 3. It's a good practice to check if the file exists and if it contains data before attempting to manipulate it. Here's your code with some modifications and comments: # code start <?php // Check if the user cookie is set and the file exists if(isset($_COOKIE['user']) && file_exists($_COOKIE['user'].'1234.txt')) { // Load the contents of the file $filename = $_COOKIE['user'].'1234.txt'; $old_list = file_get_contents($filename); // Create a DOMDocument object and load the HTML content $temp_list = new DOMDocument(); $temp_list->loadHTML($old_list); // Find the element by its ID $sector = $temp_list->getElementById('first_list'); // Check if the element with the specified ID exists if ($sector) { // Create a document fragment and append the new div element $fragment = $temp_list->createDocumentFragment(); $fragment->appendXML('<div>Hello, world!</div>'); $sector->appendChild($fragment); // Get the modified HTML content $new_html = $temp_list->saveHTML(); // Save the modified HTML content back to the file file_put_contents($filename, $new_html); echo "Element successfully inserted!"; } else { echo "Element with ID 'first_list' not found."; } } else { echo "User cookie is not set or file does not exist."; } ?> # code end
24th Feb 2024, 8:48 PM
Mihaly Nyilas
Mihaly Nyilas - avatar
0
possible you didn't check if the element with the specified ID exists
22nd Feb 2024, 9:56 PM
Mihaly Nyilas
Mihaly Nyilas - avatar
0
Mihaly Nyilas file .txt contains just html code <div id="first_list"></div> <div id="s_list"></div> <div id="t_list"></div> mb I'm using getElementById incorrectly? how can this be fixed?
22nd Feb 2024, 10:11 PM
W G
W G - avatar
0
now I tried more simple code, but still nothing worked.. nothing is saved $filename = $_COOKIE['user'].'1234.txt'; $old_list = file_get_contents($filename); $doc = new DOMDocument(); $doc->loadHTML($old_list); $f = $doc->createDocumentFragment(); $f->appendXML("<foo>text</foo><bar>text2</bar>"); $doc->documentElement->appendChild($f); echo $doc->saveHTML();
22nd Feb 2024, 10:17 PM
W G
W G - avatar
0
and here is the rest of the help/suggestion: Make sure that the file 'file.txt' contains valid HTML code with a <div> element having the ID 'first_list'. This script will attempt to insert the <div>Hello, world!</div> element into the 'first_list' <div> if it exists and the necessary conditions are met. I hope this helps. you can use some AI tools for free. it said what I wrote in my first comment.
24th Feb 2024, 8:51 PM
Mihaly Nyilas
Mihaly Nyilas - avatar