handling checked checkboxes PHP

前端 未结 3 1952
一个人的身影
一个人的身影 2021-01-25 01:47

I have a table that takes data from the database like so: (Is not a form)

if (mysql_num_rows($result)) {
        echo \"
相关标签:
3条回答
  • 2021-01-25 01:57

    $_REQUEST[ 'mark' ] will be an array of all of the checked boxes.

    0 讨论(0)
  • 2021-01-25 02:06

    You would need to do some AJAX or a form submit to pass that checkbox data to PHP for processing.

    But before you can do that, you would need to include a value attribute on those checkboxes. Currently, $_POST['mark'] (if you used POST to submit the form) would be a 0-based array of the checked checkboxes (and only the checked ones).

    I would recommend outputting the user id as the value of checkbox to help out with identifying the marked users.

    Then you could do

    foreach ($_POST['mark'] as $marked) {
        // $marked would contain the value attribute of the checked checkboxes
        // and you could run a SQL query for each value of $marked.
    }
    
    0 讨论(0)
  • 2021-01-25 02:12
    foreach($_REQUEST['mark'] as $value){
        echo "$value was selected\n <br />";
    }
    

    if you want to know which one wasn't selected then store all possible selections into an array and walk ofer this array and do someting like

    foreach($poss_select as $key=>$val){
        if(!in_array($val,$_REQUEST['mark']){
            $not_selected[$key] = $value;
        }else{
            deleteRow($value);
        }
    } 
    
    0 讨论(0)
提交回复
热议问题