Jump to content

Deleting XML-entries with checkboxes echoed by foreach


Recommended Posts

Hi! I have a page on which you can alter an XML file containing information as follows:












Each product id, name and price are echoed through a for-each loop as text inputs which you can alter and save, thus changing the XML-file contents. For each product, there is also a checkbox with which you are supposed to use for deletion. If a product's checkbox is checked and the save button is pressed, I want the certain product which had it's checkbox checked to be deleted. Take a look at the attached image to see the output of the echo.


Here is the problem: If you check a box, no matter which one, product number 1 gets deleted. If you check two boxes (again, whichever you like), product 1 and 2 gets deleted, etc. This is the code for the checkbox in the foreach loop;


echo "<input type='checkbox' id='checkbox' name='deleteProduct[]'  />Delete";


There doesn't seem to be any difference namewise between the checkboxes for some reason. If I alter the checkbox name to <b>name='deleteProduct[2]'</b>, the second entry gets deleted when you press the save button, no matter which box you check.


I know that I'm not revealing much of the code here, but that's because the majority of it doesn't really have anything to do with the problem. If anyone would take time to help me, I'd gladly show you all of the code in a PM.


Thanks in advance!


Link to comment
Share on other sites

Hey guys,


I solved it by doing the following:


$allProducts = $DOM->getElementsByTagName('products');

$count = $allProducts->getElementsByTagName('product')->length;


for ($i=0; $i<=($count); $i++)


echo "<br><input type='checkbox' id='checkbox' name='deleteProduct[".$i."]' />Delete entry ".$i."</br>";



The deletion code was so confusing that solving it this way was quite relieving.


Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.