12

i am posting an array of checkboxes. and i cant get it to work. i didnt include the proper syntax in the foreach loop to keep it simple. but it is working. i tested in by trying to do the same thing with a text field instead of a checkbox and it worked with the textfield.

<form method="post">
<?php 
foreach{
echo'
<input id="'.$userid.'" value="'.$userid.'"  name="invite[]" type="checkbox">
<input type="submit">';
}
?>
</form>

here is the part that is not working. it is echoing 'invite' instead of array.

<?php
    if(isset($_POST['invite'])){
$invite = $_POST['invite'];
echo $invite;
}
6
  • $invite is an array. Try print_r($invite); instead of echo Commented May 18, 2012 at 15:22
  • 2
    Do a print_r($_POST); and see what is actually set. Commented May 18, 2012 at 15:24
  • it says 'invite' when i do print_r Commented May 18, 2012 at 15:24
  • Try adding multiple checkbox named 'invite[]'. You'll get the output as an array. And you shouldn't be using <Form ** inside the loop as there'll always be single instance of invite in each form. Commented May 18, 2012 at 15:25
  • I think your PHP code is still wrong. Your foreach shouldn't be inside your echo and your HTML is inside of you <?PHP ?> tags. Commented May 18, 2012 at 15:40

5 Answers 5

25

Your $_POST array contains the invite array, so reading it out as

<?php
if(isset($_POST['invite'])){
  $invite = $_POST['invite'];
  echo $invite;
}
?>

won't work since it's an array. You have to loop through the array to get all of the values.

<?php
if(isset($_POST['invite'])){
  if (is_array($_POST['invite'])) {
    foreach($_POST['invite'] as $value){
      echo $value;
    }
  } else {
    $value = $_POST['invite'];
    echo $value;
  }
}
?>
Sign up to request clarification or add additional context in comments.

7 Comments

when i tried this. i got Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\social_learning\site_pages\groups_page.php on line 95
Can you copy/paste the full results of an echo '<pre>'; print_r($_POST); echo '</pre>';
I edited my answer to add a check of whether the invite value is an array or string. If you only pass one checked invite checkbox it will be a string value. That may fix the error you are receiving.
here is the <pre><pre> results Array ( [invite] => Invite [rememberme] => undefine )
i tried the edited for loop. and i still got an undefined variable. Notice: Undefined variable: value in C:\xampp\htdocs\social_learning\site_pages\groups_page.php on line 99 i think the issue has to be with the data before it is being POSTED. but when i inspect the element. the data in the html is all correct. so the problem is somewhere from before the html is sent.
|
16

I just used the following 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
    if(isset($_POST['invite'])){
        $invite = $_POST['invite'];
        print_r($invite);
    }
?>

When I checked both boxes, the output was:

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

I know this doesn't directly answer your question, but it gives you a working example to reference and hopefully helps you solve the problem.

1 Comment

thanks. my code must have some bug, type or something i am not seeing. the weird part is that i tried this exact same thing with a text field, and it worked. so the issue is related specifically to the checkbox.
5

Check out the implode() function as an alternative. This will convert the array into a list. The first param is how you want the items separated. Here I have used a comma with a space after it.

$invite = implode(', ', $_POST['invite']);
echo $invite;

1 Comment

many thanks, sir! this just helped me save about 1 hour of research. i am LOVING the implode right now.
0
// if you do the input like this
<input id="'.$userid.'" value="'.$userid.'"  name="invite['.$userid.']" type="checkbox">

// you can access the value directly like this:
$invite = $_POST['invite'][$userid];

Comments

0

Because your <form> element is inside the foreach loop, you are generating multiple forms. I assume you want multiple checkboxes in one form.

Try this...

<form method="post">
foreach{
<?php echo'
<input id="'.$userid.'" value="'.$userid.'"  name="invite[]" type="checkbox">
<input type="submit">';
?>
}
</form>

2 Comments

Nice catch. I would even move the submit button outside the loop as well.
sorry, that was a mistake i made when i typed it in the question. the code isnt really like that.

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.