简体   繁体   中英

add all checkbox values to array

My html code :

<form method="post">
    <input id="user1" value="user1"  name="invite[]" type="checkbox">
    <input id="user2" value="user2"  name="invite[]" type="checkbox">
    <input type="submit">
</form>

PHP code is :

if(isset($_POST['invite'])){
    if (is_array($_POST['invite'])) {
        foreach($_POST['invite'] as $value){
            $list = array($value);
            $fh = print_r($list, true);
        }
    } 
}

And the print_r output is :

Array ( [0] => user1 ) 
Array ( [0] => user2 )

My required output is: Array ( [0] => user1 [1] => user2 )

I'm working from the last 36 hours and googled but no luck. What is the error in my code or is there any better way to achieve checkbox values into an array(the actual values in my code are email id"s)

Thanks in advance

$_POST['invite'] already is Array ( [0] => user1 [1] => user2 ) . What you're doing is looping through it then putting the value (eg user1 ) into a new array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM