I'm attempting to add checkboxes to a simple form that I've used for years.

Here's the html markup;

<input type="checkbox" checked="checked" value="Latest Offers" name="ch1"/><label for="latest-events">Latest Offers</label>

<input type="checkbox" checked="checked" value="Free Tickets" name="ch2"/><label for="free-tickets">Free Tickets</label>

<input type="checkbox" checked="checked" value="Special Offers" name="ch3"/><label for="special-offers">Special Offers</label>

<input type="checkbox" checked="checked" value="Newsletter" name="ch4"/><label for="email-newsletter">E-mail Newsletter</label>

And the php script (the parts I've added to it)

$ch1 = 'checked';
$ch2 = 'checked';
$ch3 = 'checked';
$ch4 = 'checked';

 if (isset($_POST['ch1'])) {
$ch1 = $_POST['ch1'];

if ($ch1 = = 'Latest Offers') {
$ch1 = 'checked';
}
}

if (isset($_POST['ch2'])) {
$ch2 = $_POST['ch2'];

if ($ch2 = = 'Free Tickets') {
$ch2 = 'checked';
}
}

if (isset($_POST['ch3'])) {
$ch3 = $_POST['ch3'];

if ($ch3 = = 'Special Offers') {
$ch3 = 'checked';
}
}

if (isset($_POST['ch4'])) {
$ch4 = $_POST['ch4'];

if ($ch4 = = 'Newsletters') {
$ch4 = 'checked';
}
}

if ($name_field != null) {
  $complete = "\n
    Hapsburg Sign Up Form \n \n
    From: $name_field \n \n
    Address: $address_field \n \n
    Telephone: $tel_field \n \n
    Email: $email_field \n \n
    Offers: $ch1 \n $ch2 \n $ch3 \n $ch4";

Obviously there's also all the submit stuff in there, and the checking for the other inputs etc.

So is there something glaringly obviously I've missed out on?

7 answers

2
points
This was chosen as the best answer

What is the problem? A guess is that you get the words 'checked' in your 'Offers' part?

A different appraoch could be:

<input type="checkbox" checked="checked" value="Latest Offers" name="ch[]"/><label for="latest-events">Latest Offers</label>

<input type="checkbox" checked="checked" value="Free Tickets" name="ch[]"/><label for="free-tickets">Free Tickets</label>

<input type="checkbox" checked="checked" value="Special Offers" name="ch[]"/><label for="special-offers">Special Offers</label>

<input type="checkbox" checked="checked" value="Newsletter" name="ch[]"/><label for="email-newsletter">E-mail Newsletter</label>

And with PHP do something like:

<?php
...
$offers = "None";
if(isset($_POST["ch"]))
    $offers = implode(", ", $_POST["ch"]);

...

 $complete = "\n
    Hapsburg Sign Up Form \n \n
    From: $name_field \n \n
    Address: $address_field \n \n
    Telephone: $tel_field \n \n
    Email: $email_field \n \n
    Offers: $offers";
?>
Answered over 7 years ago by Dan Storm
1
point

A little tip on the side (as your code should be good to go):

If you give the for value of a label the same value as the id of the input field (or other form element for that matter) they will be linked. This way if a user clicks on the label (instead of the checkbox) of the matching element the element (in this case) will be checked.

<input type="checkbox" checked="checked" value="Lates Offers" name="ch[]" id="Latest-Offers"/><label for="Latest-Offers">Latest Offers</a>
Answered over 7 years ago by Jesse Vlasveld
0
points

Works like a charm, thanks so much Dan! Really simple method, too!

Answered over 7 years ago by Russell Bishop
0
points

Thanks for the advice Jesse, I wasn't aware of that functionality! Is that ending tag an </a>, or did you mean </label>?

lar3ry, what is the reasoning behind your <label><input />Label<label> idea? it's that not semantic?

Answered over 7 years ago by Russell Bishop
0
points

Nesting the <input> within the <label> does the same as what Jesse suggests, linking them by having the for-attribute of the label the same as the id-attribute of the input element. It's just two ways of achieving the same goal.

Answered over 7 years ago by Joost van der Borg
  • Although some browsers (IE) don't support the implicit linking by wrapping (you need to specify a for attribute in the label and corresponding ID in the field). Ed Eliot over 6 years ago
Jakob 17
0
points

Well i agree with most of the written parts the only thing i want to add is that double quotes are slower that single quotes since they will allow PHP to interpret variables within the string and since PHP is not using any type definitions (you may can add them which adds a little more speed to your code) it will alway try to get the type of variable you used.

this is just for bigger projects where milli-seconds do count

i have to add that \n or any escaped system interpreted value will need a double quote to work out and for comparisons which are implied in your code source you should use some alpha numeric values or at least don't use spaces and keep them lowercase value="Latest Offers" should be more like value="latest_offers" the reason is mainly that comparisons with strings are slower and typos are often overlooked .... anyhow before i get to much into this and write a performance essay these are just small tips which will be useful .... so just keep in mind what steve jobs said "you don't save just one second, you save one second for every user"

Answered over 7 years ago by Jakob
lar3ry 0
-2
points

I've found that the following makes things a bit easier:

 <label><input type="checkbox" checked="checked"  value="Latest Offers" name="ch[]" id="Latest Offers" />Latest Offers</label>

(In other words, nest the <input> within the <label> tags)

Answered over 7 years ago by lar3ry
  • As above, in IE nesting won't work. The association won't be created. Ed Eliot over 6 years ago