未定义的索引&为foreach()+表单提供的参数无效

时间:2016-08-29 13:41:33

标签: php

由于某些原因我不断收到以下错误:

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

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

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

谢谢:)

<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>

1 个答案:

答案 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>