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!

Multiple checkbox values

Status
Not open for further replies.

MarcLodge

Programmer
Feb 26, 2002
1,886
GB
Hi All,
I've been struggling with this problem for some time and have yet to reach a successful conclusion despite much help. I'm going to post this in HTML, Javascript, and PH forums in hope that SOMEBODY might be able to solve it.

The Objective
To have a number of identically named checkboxes on a page, that indicate an interest in a particular summer camp. The user can check more than one, but must check at least one. The checkbox is validated by a piece of Javascript on a submit button to ensure at least one is checked. The data is then processed by a PHP script which sends an e-mail.

The Problem
I cannot get both the Javascript AND the PHP to work. I can either one, but not both.

The HTML
<INPUT TYPE=&quot;checkbox&quot; NAME=&quot;Camps&quot; VALUE=&quot;Camp A&quot;>
<INPUT TYPE=&quot;checkbox&quot; NAME=&quot;Camps&quot; VALUE=&quot;Camp B&quot;>
or
<INPUT TYPE=&quot;checkbox&quot; NAME=&quot;Camps[]&quot; VALUE=&quot;Camp A&quot;>
<INPUT TYPE=&quot;checkbox&quot; NAME=&quot;Camps[]&quot; VALUE=&quot;Camp B&quot;>

The Javascript
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
}

The PHP
$Camps = $HTTP_POST_VARS[&quot;Camps&quot;]);
or
$Camps = implode(&quot;,&quot;, $HTTP_POST_VARS[&quot;Camps&quot;]);

What Happens?
If in the HTML I leave out the square brackets and in the PHP leave out the implode, the Javascript works fine and validates the form correctly, but the PHP script only returns the last value of the checkbox. If I add the square brackets and the implode, the PHP happily passes all values to the email, but the Javascript stops working.

I've messed about with this trying as many different combinations as I can think of, but just can't seem to get it to work how I want. I am so fed up with it, I'm considering allowing no boxes to be checked. Unless anybody has any ideas of what I'm doing wrong.

Grateful thanks in anticipation.
Marc
 
First of all, what version of PHP are you running? ______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
I have absolutely no idea! The script is one that was provided to me that I have tailored. The full text is:

<?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 = implode(&quot;,&quot;, $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;
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top