Forum Moderators: coopster

Message Too Old, No Replies

Checkbox value on submission

Checkbox

         

Map_Elixir

2:49 pm on Jun 26, 2008 (gmt 0)

10+ Year Member



I have a contact us form. I am doing self submission of the form and doing validation.

I have checkboxes on the form.

How can I retain the checked checkboxes, when the form is submitted and the required field message is displayed.

In the form

<input type="checkbox" value="Planning" name="services[]"> Planning
<input type="checkbox" value="two" name="services[]"> two
<input type="checkbox" value="three" name="services[]"> three

php code to access the services and send mail

if(isset($_POST['services']))
{
foreach($_POST['services'] as $value)
{
$check_msg .= "Services: $value\n";

}
}

In the form I am doing required field validation for various fields. When the form is submitted the checked boxes are not retained.

How can I achieve this?

Thanks a lot.

Sai

RonPK

4:02 pm on Jun 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




<input type="checkbox" value="Planning" name="services[]"<?php
if (in_array('Planning', $_POST['services'])) {
echo ' checked';
}
?>>

I hope that's what you're after.

milocold

8:52 am on Jun 27, 2008 (gmt 0)

10+ Year Member



Hi Map_Elixir,

If your trying to get the checkbox values AFTER the form is submitted this may help ya figure out what's going on.

[edit] No need for my code sample [/edit]

...But from the code you show I don't see anything intrinsically wrong. You may want to run a test without the form validation to discover if the problem lies there.

M. Cold

Map_Elixir

10:20 am on Jun 27, 2008 (gmt 0)

10+ Year Member



Dear RonPK and milocold

Thanks for your reply.

RonPK gave the code, I exactly wanted. You made it look so simple. Thanks.

One more question. I want to know, whether the way I am doing is right or there is a better way to do it.

Checkboxes

<input type="checkbox" value="Planning & Strategy" name="services[]"> Planning & Strategy

Getting the values to email

if(isset($_POST['services']))
{
foreach($_POST['services'] as $value)
{
$service_msg .= "$value<br/>";

}
}
I want to know whether it is neccesary to put the complete "Planning & Strategy" in the value field or their is a better way to do it.

Say I just put value="planning" and get "Planning & Strategy" while emailing in $service_msg.

Please guide.

Thanks.

Sai