PDA

View Full Version : Dealing with checkboxes in a server side script



Johnny Gulag
09-26-2006, 09:21 AM
Hello

How does one make a server side script for a form with ALOT of checkboxes?

Does each checkbox need an ID? Then add each ID to the server side script as I would with an input?

Or is there an easier way?

KelliShaver
09-26-2006, 09:59 AM
Depends on what you're doing.

If you have a list of items, and you want to have a checkbox to delete each one, for instance, you can do this:


<input type="checkbox" name="deletethis[]" value="item1" />
<input type="checkbox" name="deletethis[]" value="item2" />
<input type="checkbox" name="deletethis[]" value="item3" />


<?php
foreach($_POST['deletethis'] as $val) {
// code to delete whatever it is with that ID.
}
?>

Johnny Gulag
09-26-2006, 11:06 AM
Thanks for the response, I am just trying to send the selected checkboxes value(s) in an email.

Todd W
09-26-2006, 12:16 PM
Yep, use an array.

I do the above method works great.

Johnny Gulag
09-26-2006, 02:10 PM
Hello

Thanks to you both for the help but as a PHP halfwit I am dumbfounded. :p

I have this:

<input class="formCheck" type="checkbox" value="Argyle" name="cities[]"> Argyle<br />
<input class="formCheck" type="checkbox" value="Aubrey" name="cities[]"> Aubrey<br />
<input class="formCheck" type="checkbox" value="Bartonville" name="cities[]"> Bartonville<br />

Though there are alot more, 118 I think.

How do I use the PHP snippet above to capture the selected ones by name and list them in the email. I have this:

<?php
if (!$_POST['name'] || !$_POST['address'] || !$_POST['city'] || !$_POST['workphone']) {
header('Location: http://www.somesite.com/oops.html');
} else {
$message = <<<EOF

Submitted by:

Name: {$_POST['name']}
Company Name: {$_POST['company']}
Address: {$_POST['address']}
Address 2: {$_POST['address_2']}
City/State/Zip: {$_POST['city']}
Work Phone: {$_POST['workphone']}
FAX: {$_POST['fax']}
Email: {$_POST['email']}
Unit Size: {$_POST['unitSize']}
Occupants: {$_POST['occupants']}
Length: {$_POST['lengthoflease']}
Purchase: {$_POST['purchaseHome']}

My City Choices are:

($_POST['cities'])

Additional Comments:

{$_POST['form_comment']}

EOF;

mail('email address here', 'subject line here', $message, "From: {$_POST['email']}\n");
header('Location: http://www.somesite.com/thankyou.html');
}
?>

Don't laugh too hard as I said I am a programming halfwit and this is the best I can do. Everything works accept the checkboxes.

I get this in the email:



Submitted by:

Name: Me
Company Name: company
Address: 1234 fake st
Address 2: na
City/State/Zip: city, state, zip
Work Phone: 555-555-5555
FAX: na
Email: an email address
Unite Size: twoBedroom
Occupants: five
Length: sixMonths
Purchase: no

My City Choices are:

Array

Additional Comments:

testing

Thanks,

Johnny Gulag
09-26-2006, 11:30 PM
I figured it out, thanks though to anyone who tried and to teh 2 who responded.