Get $_POST from multiple checkboxes

前端 未结 5 1376
醉酒成梦
醉酒成梦 2020-11-22 01:03

I have 1 form in with multiple checkboxes in it (each with the code):



        
相关标签:
5条回答
  • 2020-11-22 01:11

    Edit To reflect what @Marc said in the comment below.

    You can do a loop through all the posted values.

    HTML:

    <input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
    <input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
    <input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
    

    PHP:

    foreach($_POST['check_list'] as $item){
      // query to delete where item = $item
    }
    
    0 讨论(0)
  • 2020-11-22 01:19

    Set the name in the form to check_list[] and you will be able to access all the checkboxes as an array($_POST['check_list'][]).

    Here's a little sample as requested:

    <form action="test.php" method="post">
        <input type="checkbox" name="check_list[]" value="value 1">
        <input type="checkbox" name="check_list[]" value="value 2">
        <input type="checkbox" name="check_list[]" value="value 3">
        <input type="checkbox" name="check_list[]" value="value 4">
        <input type="checkbox" name="check_list[]" value="value 5">
        <input type="submit" />
    </form>
    <?php
    if(!empty($_POST['check_list'])) {
        foreach($_POST['check_list'] as $check) {
                echo $check; //echoes the value set in the HTML form for each checked checkbox.
                             //so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
                             //in your case, it would echo whatever $row['Report ID'] is equivalent to.
        }
    }
    ?>
    
    0 讨论(0)
  • 2020-11-22 01:21

    you have to name your checkboxes accordingly:

    <input type="checkbox" name="check_list[]" value="…" />
    

    you can then access all checked checkboxes with

    // loop over checked checkboxes
    foreach($_POST['check_list'] as $checkbox) {
       // do something
    }
    

    ps. make sure to properly escape your output (htmlspecialchars())

    0 讨论(0)
  • 2020-11-22 01:23
    <input type="checkbox" name="check_list[<? echo $row['Report ID'] ?>]" value="<? echo $row['Report ID'] ?>">
    

    And after the post, you can loop through them:

       if(!empty($_POST['check_list'])){
         foreach($_POST['check_list'] as $report_id){
            echo "$report_id was checked! ";
         }
       }
    

    Or get a certain value posted from previous page:

    if(isset($_POST['check_list'][$report_id])){
      echo $report_id . " was checked!<br/>";
    }
    
    0 讨论(0)
  • 2020-11-22 01:24

    It's pretty simple. Pay attention and you'll get it right away! :)

    You will create a html array, which will be then sent to php array. Your html code will look like this:

    <input type="checkbox" name="check_list[1]" alt="Checkbox" value="checked">
    <input type="checkbox" name="check_list[2]" alt="Checkbox" value="checked">
    <input type="checkbox" name="check_list[3]" alt="Checkbox" value="checked">
    

    Where [1] [2] [3] are the IDs of your messages, meaning that you will echo your $row['Report ID'] in their place.

    Then, when you submit the form, your PHP array will look like this:

    print_r($check_list)

    [1] => checked [3] => checked

    Depending on which were checked and which were not.

    I'm sure you can continue from this point forward.

    0 讨论(0)
提交回复
热议问题