2

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

2
  • 1
    Try $_POST['language'] in place of $_POST['language[]'] ? Commented Jun 19, 2015 at 18:58
  • This worked great - see below. Thanks again - I voted you up as well. Commented Jun 19, 2015 at 19:03

2 Answers 2

5

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']);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot - let me try this out.
Awesome - this works great. Life can be so easy sometimes. :) Thanks a lot - will accept as soon as I can.
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 ?
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
0

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,", ");

Comments

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.