Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am new to PHP and am looking for some help with the following:

I have a large HTML form with various inputs, selects and textareas. On submit I pass all field values to another PHP page to create an email with them.

This works as intended for text inputs, radio buttons, selects, textareas etc. but not for checkboxes.

In the form I have multiple checkboxes that all look as follows using the same name:

<input type='checkbox' class='someClass' id='language1' name='language[]' value='de - German' />
<input type='checkbox' class='someClass' id='language2' name='language[]' value='en - English' />
<input type='checkbox' class='someClass' id='language3' name='language[]' value='fr - French' />
<!-- ... -->

On the PHP side I tried the following but if I then use $languages for my email body it shows as blank (while all other fields appear correctly):

$languages = implode(',', $_POST['language[]']);

What I am trying to do is to save all values from the $_POST["language"] array in one variable, separated with commas (and no comma at the end).

Can someone help me with this ?

Many thanks in advance

share|improve this question
1  
Try $_POST['language'] in place of $_POST['language[]'] ? – Maximus2012 Jun 19 '15 at 18:58
    
Thanks for this ! Will try. – TaneMahuta Jun 19 '15 at 18:59
    
This worked great - see below. Thanks again - I voted you up as well. – TaneMahuta Jun 19 '15 at 19:03
up vote 3 down vote accepted

If you use anyname[] in the form, PHP will translate it to an array, but without the [] in the name. So this should work:

$languages = implode(',', $_POST['language']);

Note that unchecked checkboxes are not posted, so if you check none, no value is posted and $_POST['language'] will not be set. You would have to check for that.

$languages = 'No languages selected';
if (isset($_POST['language'])){
  $languages = implode(',', $_POST['language']);
}
share|improve this answer
    
Thanks a lot - let me try this out. – TaneMahuta Jun 19 '15 at 18:59
    
Awesome - this works great. Life can be so easy sometimes. :) Thanks a lot - will accept as soon as I can. – TaneMahuta Jun 19 '15 at 19:02
    
Thanks for the update as well. One question on this: It should be rare that no checkbox will be checked here (since one of them is checked by default). Can it cause any issues if I don't check if any value was posted for this, i.e. do I have to do this ? – TaneMahuta Jun 19 '15 at 19:07
    
Yes. If you just read $_POST['language'] if it doesn't exist, it will cause a run-time error and your script will end prematurely. You think it should be rare, but it's just user input. One could easily press a button without first checking a box, and if you think it should almost never happen, it will probably happen after five minutes. ;) I can tell from experience. :p – GolezTrol Jun 19 '15 at 20:57

HTML

<input type='checkbox' class='someClass' id='language1' name='language[]' value='1' />
<input type='checkbox' class='someClass' id='language2' name='language[]' value='2' />
<input type='checkbox' class='someClass' id='language3' name='language[]' value='3' />
<!-- ... -->

PHP

$language_ = array(1=>'de - German',2=>'en - English',3=>'fr - French');
if (!empty($_GET['language'])) {
     foreach ($_GET['language'] as $l) {
                $lan0 .= $language_[$l].', ';
            }
echo rtrim($lan0,", ");
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.