0

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

1
  • What is the output of print_r($_POST['invite']); ? Commented Jun 6, 2013 at 15:00

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

if(isset($_POST['invite'])){ if (is_array($_POST['invite'])) { foreach($_POST['invite'] as $value){ $list = $_POST['invite']; output is : Array ( [0] => user1 [1] => user2 ) Array ( [0] => user1 [1] => user2 ) $fh = print_r($list, true); echo "<br/>". $fh; } } }
But still i'm getting Array ( [0] => user1 [1] => user2 ) Array ( [0] => user1 [1] => user2 )
@user2430278 I don't mind this harshly, but you need to read up on the basics of PHP. In a foreach loop, your provide your array (in this case $_POST['invite']) then you tell it what variable to hold each array indexes value in. In your case, you've said to put it into $value, so $value contains user1 and user2.
Done Working.. Thank you (@Prisoner)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.