Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script doesn't process all checkboxes 2

Status
Not open for further replies.

MarcLodge

Programmer
Feb 26, 2002
1,886
GB
Hi All,
Can anybody help? I have an HTML form and a PHP script. The form has various fields and a number of checkboxes. I am only getting one value for the checkboxes when the user ticks more than one.

The HTML and the script can be found at thread215-284789 . I'd appreciate any help at all as I'm completely stuck!
Thanks
Marc
 
Try making the item an array by adding the [] to the name:

<INPUT TYPE=&quot;radio&quot; NAME=&quot;TShirtSize&quot; VALUE=&quot;S&quot;>S
<INPUT TYPE=&quot;radio&quot; NAME=&quot;TShirtSize[]&quot; VALUE=&quot;M&quot;>M
<INPUT TYPE=&quot;radio&quot; NAME=&quot;TShirtSize[]&quot; VALUE=&quot;L&quot;>L
<INPUT TYPE=&quot;radio&quot; NAME=&quot;TShirtSize[]&quot; VALUE=&quot;XL&quot;>XL

this places all the values in an array that can be looked through, changes and played with in code to make the info useful.

The above will give you (assume user checks M and XL) an array of M,XL

when you call this code:

$TShirtSize = $HTTP_POST_VARS[&quot;TShirtSize&quot;];

so $TShirtSize = 'M,XL'

hth Bastien

There are many ways to skin this cat,
but it still tastes like chicken
 
Thanks Bastien, but it's STILL not working!! In the e-mail that is sent, instead of having a value I'm getting the word 'Array'!! I'm sure it's something very simple and I'm just missing it. The HTML now reads:

<INPUT TYPE=&quot;checkbox&quot; NAME=&quot;Camps[]&quot; VALUE=&quot;Henley&quot;>
<INPUT TYPE=&quot;checkbox&quot; NAME=&quot;Camps[]&quot; VALUE=&quot;Seville Week 1&quot;>
<INPUT TYPE=&quot;checkbox&quot; NAME=&quot;Camps[]&quot; VALUE=&quot;Seville Week 2&quot;>

and the script reads:

?PHP $fmt_Response=implode(&quot;&quot;, file(&quot;response.htt&quot;));
$fmt_Mail=implode(&quot;&quot;, file(&quot;Mail.htt&quot;));

while(list($Key, $Val)= each($HTTP_POST_VARS)) {
$fmt_Response=str_replace(&quot;{$Key}&quot;, $Val, $fmt_Response);
$fmt_Mail=str_replace(&quot;{$Key}&quot;, $Val, $fmt_Mail);
}

$name = $HTTP_POST_VARS[&quot;name&quot;];
$address = $HTTP_POST_VARS[&quot;address&quot;];
$phone = $HTTP_POST_VARS[&quot;phone&quot;];
$email = $HTTP_POST_VARS[&quot;email&quot;];
$age = $HTTP_POST_VARS[&quot;age&quot;];
$sex = $HTTP_POST_VARS[&quot;sex&quot;];
$Experience = $HTTP_POST_VARS[&quot;Experience&quot;];
$TShirtSize = $HTTP_POST_VARS[&quot;TShirtSize&quot;];
$Comments = $HTTP_POST_VARS[&quot;Comments&quot;];
$Camps = $HTTP_POST_VARS[&quot;Camps&quot;];

$mailText = &quot;$fmt_Mail \n\t Name: $name \n\t Address: $address \n\t Phone Number: $phone \n\t E-Mail: $email \n\t Sex: $sex \n\t Experience: $Experience \n\t T-ShirtSize: $TShirtSize \n\t Comments: $Comments \n\t Interested in Camps: $Camps &quot;;

mail($HTTP_POST_VARS[&quot;recipient&quot;], $HTTP_POST_VARS[&quot;subject&quot;], $mailText );
echo $fmt_Response;
?>

 
Instead of
$Camps = $HTTP_POST_VARS[&quot;Camps&quot;];
put
$Camps = implode(&quot;,&quot;, $HTTP_POST_VARS[&quot;Camps&quot;]);
This would give you a comma separated list of all the checkboxes that were checked. //Daniel
 
daniel and Bastien, thanks ever so, and for that you get my star as I now get multiple values in my e-mail..... but.... my javascript that ensures a checkbox has been entered has subsequently packed up!!! If anybody has any thoughts, because I've had a good play, and I can't work it out...
The piece of javascript in question (which worked before I added the square brackets):

Campswitch = 0
for (i=0; i<regform.Camps.length;i++)
{ if (regform.Camps.checked)
{ Campswitch = 1
}
}
if (Campswitch == 0)
{ alert(&quot;Please select one or more Camps and dates that you are interested in&quot;)
return false
}
 
web browsers do not submit the values of un-checked boxes, If you have 2 checkboxes:
<input type=checkbox name=first value=1>
<input type=checkbox name=second value=1>

and u check the first only $_POST['first'] will be set, no mention of second.

A short work around is to use a hidden form var:
<input type=hidden name=first value=0>
<input type=checkbox name=first value=1>
<input type=hidden name=second value=0>
<input type=checkbox name=second value=1>

This will make the form submit 0 or 1 for on or off :) --BB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top