2016-08-29 39 views
0

由于某种原因,总是收到以下错误提供参数无效:未定义指数&为的foreach()+形式

- 未定义的索引:除去在第5行

- 提供的foreach无效的参数()第6行

你有什么想法我做错了吗?

谢谢:)

<html> 
<body> 
<?php 
    if(isset($_POST['remove_yes'])){  
    echo $_POST['remove']; 
    foreach($_POST['remove'] as $item){   
     echo ('You have just removed location id: '.$item . '<br   />');    
    }     
    } 
    elseif(isset($_POST['remove_no'])){ 
    echo 'No data removed!';   
    } 
    ?>     
    <form method="post"> 
    <input type="submit" class="button" name="submit_remove"  value="Remove"> 
    <input type="checkbox" name="remove[a]"> 
    <input type="checkbox" name="remove[b]"> 
    </form>  
    <?php 
    if(isset($_POST['submit_remove'])){ //confirmation 
    ?>    
    <form method="post"> 
    <table> 
     <tr> 
     <th>Do you want to really remove data?</th> 
     </tr> 
     <tr> 
     <td><input type="submit" value = "Yes" name = "remove_yes"></td> 
     <td><input type ="submit" value = "No" name="remove_no"></td> 
     </tr> 
    </table>    
    </form>    
    <?php 
    } 
?> 
</body> 
</html> 
+0

'$ _ POST [ '删除']'是一个数组 – devpro

+0

是的,它应该是成为 –

+0

因为你有两种不同的形式。 –

回答

0

你只需要处理单个标签形式)

<html> 
<body> 
    <?php 

    if(isset($_POST['remove_yes'])){  
    foreach($_POST['remove'] as $key =>$item){   
    echo ('You have just removed location id: '.$key . '<br   />');  

    }     
    } 
    elseif(isset($_POST['remove_no'])){ 
    echo 'No data removed!';   
    } 
    ?>     
    <form method="post"> 
     <input type="submit" class="button" name="submit_remove"  value="Remove"> 
     a<input type="checkbox" <?=($_POST['remove']['a']=='on')?'checked="checked"': ''?> name="remove[a]"> 
       b<input type="checkbox" <?=($_POST['remove']['b']=='on')?'checked="checked"': ''?> name="remove[b]"> 
       <?php 
       if(isset($_POST['submit_remove'])){ 
       //confirmation 
       ?>    
       <table> 
      <tr> 
       <th>Do you want to really remove data?</th> 
      </tr> 
      <tr> 
       <td>yes 
        <input type="submit" value="Yes" name="remove_yes"> 
       </td> 
       <td>NO<input type ="submit" value="No" name="remove_no"></td> 
      </tr> 
     </table>    
    </form>    
    <?php 
    } 
    ?> 
</body></html>