-1

I have this code:

<?php
$animals = array('cat', 'dog', 'mouse', 'elephant');
for($i=0, $n = count($animals); $i++){
    echo $animals[$i]. '<input type="submit" name="remove" value="remove"/><br/>';
    if(isset($_POST['remove'])){
        unset($animals[$i]);
    }
}
?>

When I click on the remove button nothing happens. Any idea?

tkanzakic
  • 5,499
  • 16
  • 34
  • 41

2 Answers2

1

if you are going to present "remove" as the value for each of the buttons, then the easiest way is to present an array for the name attribute. Once the $_POST is sent, you can iterate through the remove array and unset anything based off of the key.

<?php
$animals = array('cat', 'dog', 'mouse', 'elephant');
if(isset($_POST['remove']) && is_array($_POST['remove'])){
    foreach($_POST['remove'] as $k=>$remove){
        unset($animals[$k]);
    }
}

echo '<form method="post" action="./">';
foreach($animals as $key=>$animal){
    echo $animal. '<input type="submit" name="remove['.$key.']" value="remove"><br/>';
}
echo '</form>';?>

Note: There are many ways to achieve what you are looking for. This is one possibility.

Samuel Cook
  • 16,620
  • 7
  • 50
  • 62
  • Thank you Samuel! Your solution is good, but the first time I press the button remove an animal is removed, the second time does not delete anything, but replaces it with the animal missing. I want every time you press the button remove an animal is removed without replacement. Sheikh Heera says I should consider javascript, what do you think? Do you have another method? – Alberto Grimaldi Dec 09 '12 at 15:28
  • @AlbertoGrimaldi: It is because of forech loop constraint in item deletion. – Hamid Araghi Sep 28 '21 at 11:47
1

This is also another approach that could be used

$animals = array('cat', 'dog', 'mouse', 'elephant');
if(isset($_POST['remove'])) $animals=array_diff($animals, array($_POST['animal']));
foreach($animals as $pet)
{
    echo'<form method="post" action="">';
    echo $pet.'<input type="submit" name="remove" value="remove"/><br/>';
    echo '<input type="hidden" name="animal" value="'.$pet.'"/>';
    echo '</form>';
}

Update: (For the JavaScript solution you can use)

$animals = array('cat', 'dog', 'mouse', 'elephant');
echo'<form name="animalForm" method="post" action="">'; // action should be your this file name, i.e. 'pets.php'
foreach($animals as $pet)
{
    echo '<label>'.$pet.'<input type="button" class="remove" value="remove"/></label><br/>';
}
echo '</form>';

And this is the JavaScript code that could be used in the head tags between script tags like

<script type="text/javascript">
    window.onload=function(){
        var btns=document.getElementsByClassName('remove');       
        for(i=0;i<btns.length;i++)
        {
            if(btns[i].type==='button' && btns[i].className==='remove')
                btns[i].onclick=remove;
        }
    };
    function remove(event){
        var e = event || window.event;
        var el = e.target || e.srcElement;
        document.animalForm.removeChild(el.parentNode);
    }
</script>

JS Demo.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • Thank @samuel and Sheikh, I tried your codes, the first time I press the button remove an animal is removed, the second time does not delete anything, but replaces it with the animal missing. I want every time you press the button remove an animal is removed without replacement. Here the pages: [link]http://designnerd.it/prova/index.php [link]http://designnerd.it/prova/index1.php – Alberto Grimaldi Dec 09 '12 at 11:05
  • @AlbertoGrimaldi, You can't do it in this way because when you press the remove button it one item to the server to remove and your array is not saved in any way. I think you can use `JavaScript` for this. – The Alpha Dec 09 '12 at 14:46
  • Ok, can you explain how I can do? I have tried many ways, but I can not find the solution! thanks again! – Alberto Grimaldi Dec 09 '12 at 15:25
  • I tried the update, but when I click on remove does not delete anything. – Alberto Grimaldi Dec 09 '12 at 16:58
  • @AlbertoGrimaldi, look at the [jsdemo](http://jsbin.com/exedas/1/edit), it's working. – The Alpha Dec 09 '12 at 17:33
  • @AlbertoGrimaldi, updated the answer and [demo here](http://jsbin.com/exedas/6/edit). It was a browser incompatibility with `getElementsByClassName`. – The Alpha Dec 09 '12 at 19:52
  • @AlbertoGrimaldi, Oops! There was a typo, [fixed it](http://jsbin.com/exedas/8/edit). – The Alpha Dec 09 '12 at 20:09
  • Now I have another question – Alberto Grimaldi Dec 09 '12 at 20:12
  • I want to submit the undeleted animals in another page... how I can do? – Alberto Grimaldi Dec 09 '12 at 20:14
  • 1
    I'm sorry to bother you, but I'm a beginner and I would like to learn as much as possible. – Alberto Grimaldi Dec 09 '12 at 20:19