Inquiescent Posted August 21, 2012 Share Posted August 21, 2012 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. 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! Quote Link to comment https://forums.phpfreaks.com/topic/267395-deleting-xml-entries-with-checkboxes-echoed-by-foreach/ Share on other sites More sharing options...
xyph Posted August 21, 2012 Share Posted August 21, 2012 Is there any reason you're using an XML file for this? You'd be much better off using something like SQLite if you need a flat-file database. Quote Link to comment https://forums.phpfreaks.com/topic/267395-deleting-xml-entries-with-checkboxes-echoed-by-foreach/#findComment-1371281 Share on other sites More sharing options...
btherl Posted August 21, 2012 Share Posted August 21, 2012 It sounds like your code already has a way to identify which checkbox is which, but it isn't working properly. You really need to post that part of the code. Quote Link to comment https://forums.phpfreaks.com/topic/267395-deleting-xml-entries-with-checkboxes-echoed-by-foreach/#findComment-1371289 Share on other sites More sharing options...
Inquiescent Posted August 22, 2012 Author Share Posted August 22, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/267395-deleting-xml-entries-with-checkboxes-echoed-by-foreach/#findComment-1371452 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.