Deleting XML-entries with checkboxes echoed by foreach

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
Inquiescent
New php-forum User
New php-forum User
Posts: 6
Joined: Tue Dec 13, 2011 8:27 pm

Tue Aug 21, 2012 1:58 pm

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

<productlist>

<product>
<id>Id</id>
<name>Name</name>
<price>Price</price>
</product>

</productlist>

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. Here's an image of what it looks like:

Image

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!

NigelRen
php-forum Fan User
php-forum Fan User
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

Tue Aug 21, 2012 2:09 pm

You don't have to put in huge amounts of your code, but the bit of code which is dealing with the deletion would probably help.

Having a quick look on Google for some sample code - have a look at how it's done here... http://stackoverflow.com/questions/4997 ... checkboxes the main thing is the use of the value attribute and if you set it to the ID - you should be able to use it to delete the corresponding item.

Acesystech
New php-forum User
New php-forum User
Posts: 35
Joined: Mon Aug 20, 2012 10:27 pm
Location: Ernakulam
Contact:

Tue Aug 21, 2012 10:01 pm


Post Reply