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

前端 未结 5 1685
眼角桃花
眼角桃花 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:47

    Here's my solution:

    With an array of checkboxes in the html like so...

    
    
    
    
    
    
    

    I then fix the posted array with this function...

    $_POST[ 'something' ] = $this->fixArrayOfCheckboxes( $_POST[ 'something' ] );
    function fixArrayOfCheckboxes( $checks ) {
        $newChecks = array();
        for( $i = 0; $i < count( $checks ); $i++ ) {
            if( $checks[ $i ] == 'off' && $checks[ $i + 1 ] == 'on' ) {
                $newChecks[] = 'on';
                $i++;
            }
            else {
                $newChecks[] = 'off';
            }
        }
        return $newChecks;
    }
    

    This will give me an array with values of either 'on' or 'off' for each (and every) checkbox.

    Note that the hidden input MUST be BEFORE the checkbox input in order for the function to work right.

提交回复
热议问题