1

I'm new here and tried to search through the existing posts before asking, but couldnt find one that worked for mine exactly that worked for fixing my form. I'm still learning this - so not sure what I've done wrong in my form.

With this form I'll attach below, I get this warning/errors' in the top of my browser:

Notice: Undefined index: checkbox_group_1 in /home/adk819/public_html/DummyWebs/DCC2/send_form_email.php on line 25
Warning: implode() [function.implode]: Invalid arguments passed in /home/adk819/public_html/DummyWebs/DCC2/send_form_email.php on line 25

Here is my html (where only the error is for - it's quite a large form :/) :

<form class="formTemplate" method="POST" action="send_form_email.php">
<div>
<fieldset><!-- Wrap checkbox/radio button groups in fieldsets -->
<legend id="casetype">Case Type</legend>
<div>
<input type="checkbox" id="casetype_1" name="checkbox_group_1[]" value="Rackmount" />
<label for="casetype_1">Unknown</label>
</div>
<div>
<input type="checkbox" id="casetype_2" name="checkbox_group_1[]" value="Injection" />
<label for="casetype_2">Injection Molded</label>
</div>
<div>
<input type="checkbox" id="casetype_3" name="checkbox_group_1[]" value="Rotationally" />
<label for="casetype_3">Rotationally Molded</label>
</div>
<div>
<input type="checkbox" id="casetype_4" name="checkbox_group_1[]" value="Rackmount" />
<label for="casetype_4">Rack Mount</label>
</div>
</fieldset>
</div>
</form>

Here is my php (for the above area):

$casetype = implode(' | ', $_POST['checkbox_group_1']);

// Construct email body

$email_message .= "Case Type: ". $casetype . "\r\n";

Here is where I found the info to make this part of the form: http://webdesy.com/how-to-create-html-php-contact-form-radio-buttons-checkboxes-dropdown-menu/ I've asked for their help - but no fix as of yet :(.

Thank you all for any help you can give me! Mary~

1
  • 1
    I get the above warning/error - IF NO CHECKBOXES ARE CHECKED ONLY. (Sorry - forgot to mention that) Commented Oct 21, 2013 at 19:03

3 Answers 3

1

try to bind your code in issset, it will execute only when checkbox variable is present as

if(is_array($_POST['checkbox_group_1']) && isset($_POST['checkbox_group_1']))
{
$casetype = implode(' | ', $_POST['checkbox_group_1']);

// Construct email body

$email_message .= "Case Type: ". $casetype . "\r\n";
}

The error is basically whenever your checkbox is not set as an array, so when you try doing implode on it PHP is unhappy because it is expecting an array.

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

1 Comment

Thank you for your answer! The response below fixed it - tho I'm not sure how it differs from yours ;). As for now.. it works! Thanks again!
0

Checkboxes which aren't checked do NOT get submitted with the form. If there are NO checkboxes checked in a particular group, then that group name will NOT be submitted, and you end up with checkbox_group_n being undefined. You need something like

if (isset($_POST['checkbox_group_1'])) {
  implode...
}

1 Comment

Thank you for your time to answer - I saw the other comment and tried it first and it WORKED!! But thanks again! :)
0

I suggest checking for the $_POST values before imploding them.

If no checkboxes in a specific group are checked, that variable will not be posted and will be undefined.

This example uses PHP's empty() and ternary operators:

$casetype = !empty($_POST['checkbox_group_1']) 
              ? implode(' | ', $_POST['checkbox_group_1'])
              : false;

EDIT:

You can also display a specific message if no options are selected in a group.
Instead of setting missing values to false, set them to something else:

$casetype = !empty($_POST['checkbox_group_1']) 
              ? implode(' | ', $_POST['checkbox_group_1'])
              : "n/a";

1 Comment

OH MY GOODNESS!! THAT FIXED IT!! Bless you, Bless you, Bless you!!! Thank you so much for your time!!!

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.