Forum Moderators: coopster

Message Too Old, No Replies

Form Checkboxes and Email

         

TymArtist

10:13 pm on Sep 21, 2005 (gmt 0)

10+ Year Member



Hello fellow users. I am new to the boards and just started visiting a few days ago. The amount of help I have received just from reading has been a lot, but now I need to make a post of my own. This has probaby been brought up before, but I need help with an html form and then getting the checkbox state to email to me. I almost have it, but not quite.

For your standard checkbox, the source code would be:
<input type="checkbox" name="product" value="pdt"> Mobile Computers <br />

'name="product"' spans throughout all the checkboxes,
while the 'value=' varies depending on the name of the checkbox they click.

To send the email, the person before me set up this page to submit to another page that goes ahead and generates the email. The lines of code for the checkboxes look something like this:
$message .= '<br />PDTs: ';
$message .= $pdt;

Please let me know what I should go about doing. I'm rather new to PHP and this is for a work situation, so any help would be appreciated. Thanks!

StupidScript

10:53 pm on Sep 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard, TymArtist!

Funny you should ask ... I've just finished coding a similar thingy.

My habit for coding checkboxes is to use a slightly different name pattern than you show in your post. I've found it makes it really easy to handle checkboxes.

Instead of

name="product"
, I would use
name="product[]"
to create a single form element which is an array. Then, on the processing page, I can simply do (using your code snippet):

foreach($_POST["product"] as $key => $pdt) {

$message .= '<br />PDTs: '. $pdt;

}

(The

$key
in this loop will always be
product[n]
where
n
is the array element's position.)

Any checkboxes not checked will not be included (as usual). Any checked checkboxes will receive their own line in

$message
.

Hope that's helpful.