How to pass an array of checked/unchecked checkbox values to PHP email generator?

前端 未结 5 1675
眼角桃花
眼角桃花 2020-12-05 22:04

I have added a checkbox to a form that the user can dynamically add rows to. You can see the form here.

I use an array to pass the values for each row to a PHP emai

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 22:51

    $mailing = array();
    foreach($_POST as $v){
        $mailing[] = trim(stripslashes($v));
    }
    

    To handle unchecked boxes it would be better to set each checkbox with a unique value:

    
    
    

    or

    
    
    

    Then have a list of the checkboxes:

    $boxes = array(1,2,3);
    $mailing = array();
    $p = array_key_exists('mailing',$_POST) ? $_POST['mailing'] : array();
    foreach($boxes as $v){
        if(array_key_exists($v,$p)){
            $mailing[$v] = trim(stripslashes($p[$v]));
        }else{
            $mailing[$v] = 'No';
        }
    }
    
    print_r($mailing);
    

    You could also use this with a number of checkboxes instead:

    $boxes = 3;
    $mailing = array();
    $p = array_key_exists('mailing',$_POST) ? $_POST['mailing'] : array();
    for($v = 0; $v < $boxes; $v++){
        if(array_key_exists($v,$p)){
            $mailing[$v] = trim(stripslashes($p[$v]));
        }else{
            $mailing[$v] = 'No';
        }
    }
    
    print_r($mailing);
    

提交回复
热议问题