Page 1 of 1

PHP Form Help with Check Boxes

Posted: Thu Jan 31, 2008 11:23 am
by eiannarino
I am having a hard time getting checkboxes to pass through to email. Only the last option clicked gets sent through the email.

Email form Code:

Code: Select all

 <form method="post" action="contactyouth.php"> 
<table width="100%" align=center> 
<tr><td colspan=3><input name="sendto" type="hidden" value="elizabethiannarino@gmail.com" /></td></tr> 
 
<tr>
  <td width="146"><strong>Name:</strong></td><td colspan="2"><input size=25 name="Name"></td></tr>
<tr>
  <td><strong>Street Address:</strong></td>
  <td colspan="2"><input size="25" name="Street_Address" /></td>
</tr>
<tr>
  <td><strong>City:</strong></td>
  <td colspan="2"><input size="25" name="City" /></td>
</tr>
<tr>
  <td><strong>State:</strong></td>
  <td colspan="2"><input size="25" name="State" /></td>
</tr>
<tr>
  <td><strong>Zip code:</strong></td>
  <td colspan="2"><input size="25" name="Zipcode" /></td>
</tr>
<tr>
  <td><strong>Phone:</strong></td>
  <td colspan="2"><input size="25" name="Phone" /></td>
</tr>
<tr>
  <td><strong>Cell Phone:</strong></td>
  <td colspan="2"><input size="25" name="Cell_Phone" /></td>
</tr>
<tr>
  <td><strong>Email:</strong></td>
  <td colspan="2"><input size="25" name="Email" /></td>
</tr> 
<tr>
  <td><strong>School:</strong></td><td colspan="2"><input size=25 name="School"></td></tr> 
<tr>
  <td><strong>Current Grade Level:</strong></td><td colspan="2"><input size=25 name="Grade_Level"></td></tr>
<tr>
  <td rowspan="10"><strong>Volunteer Opportunities</strong><strong>: </strong></td>
  <td colspan="2"><input type="checkbox" name="Volunteer_Opportunities" value="Craft Room Helper" />
Craft Room Helper</td>
  </tr>
 
<tr>
  <td colspan="2"><input type="checkbox" name="Volunteer_Opportunities" value="Drama/skit Actor" />
Drama/skit Actor</td>
</tr>
<tr>
  <td colspan="2"><input type="checkbox" name="Volunteer_Opportunities" value="Preschool Teacher's Assistant" />
Preschool Teacher&rsquo;s Assistant</td>
</tr>
<tr>
  <td colspan="2"><input type="checkbox" name="Volunteer_Opportunities" value="5 Year Olds Teacher's Assistant" />
5 Year Olds Teacher&rsquo;s  Assistant</td>
</tr>
<tr>
  <td colspan="2"><input type="checkbox" name="Volunteer_Opportunities" value="Kindergarten Teacher's  Assistant" />
Kindergarten Teacher&rsquo;s  Assistant </td>
</tr>
<tr>
  <td colspan="2"><input type="checkbox" name="Volunteer_Opportunities" value="1st Grade Teacher's Assistant" />
1st Grade Teacher&rsquo;s Assistant</td>
</tr>
<tr>
  <td colspan="2"><input type="checkbox" name="Volunteer_Opportunities" value="2nd Grade  Teacher's Assistant" />
2nd Grade  Teacher&rsquo;s Assistant</td>
</tr>
<tr>
  <td colspan="2"><input type="checkbox" name="Volunteer_Opportunities" value="3rd Grade  Teacher's Assistant" />
3rd Grade  Teacher&rsquo;s Assistant</td>
</tr>
<tr>
  <td colspan="2"><input type="checkbox" name="Volunteer_Opportunities" value="4th Grade Teacher's  Assistant" />
4th Grade Teacher&rsquo;s  Assistant</td>
</tr>
<tr>
  <td colspan="2"><input type="checkbox" name="Volunteer_Opportunities" value="5th Grade  Teacher's Assistant" />
5th Grade  Teacher&rsquo;s Assistant</td>
</tr>
 
<tr>
  <td><strong>T-Shirt Size: </strong></td>
  <td width="147"><strong>Child:
    </strong><br />
    <input type="radio" name="T-Shirt" value="Child Small" />
    Small
    <br> <input type="radio" name="T-Shirt" value="Child Medium" /> 
    Medium <br />
    <input type="radio" name="T-Shirt" value="Child Large" /> 
    Large
<br></td>
  <td width="118"><strong>Adult: </strong><br />
    <input type="radio" name="T-Shirt" value="Adult Small" />
Small <br />
<input type="radio" name="T-Shirt" value="Adult Medium" />
Medium <br />
<input type="radio" name="T-Shirt" value="Adult Large" />
Large </td>
</tr>
 
<tr><td colspan=3><p><strong>Please write a short  paragraph about why you would like to volunteer for Jesus & You camp: </strong></p></td></tr> 
<tr><td colspan=3 align=center><textarea name="Message" rows=5 cols=45></textarea></td></tr> 
<tr><td colspan=3 align=center><input type=submit name="send" value="Submit"></td></tr> 
</table> 
</form>
PHP processing page 'contactregistration.php':

Code: Select all

<?php 
$to = $_REQUEST['sendto'] ; 
$name = $_REQUEST['Name'] ;
$address = $_REQUEST['Street_Address'] ; 
$city = $_REQUEST['City'] ; 
$state = $_REQUEST['State'] ; 
$zipcode = $_REQUEST['Zipcode'] ; 
$phone = $_REQUEST['Phone'] ; 
$cellphone = $_REQUEST['Cell_Phone'] ; 
$from = $_REQUEST['Email'] ; 
$school = $_REQUEST['School'] ; 
$grade = $_REQUEST['Grade_Level'] ; 
$volunteer = $_REQUEST['Volunteer_Opportunities'] ;  
$tshirt = $_REQUEST['T-Shirt'] ;   
$headers = "From: $from"; 
$subject = "Jesus and You: Youth Volunteer Application"; 
 
$fields = array(); 
$fields{"Name"} = "\n\nName"; 
$fields{"Street_Address"} = "\n\nStreet Address"; 
$fields{"City"} = "\n\nCity"; 
$fields{"State"} = "\n\nState"; 
$fields{"Zipcode"} = "\n\nZip Code"; 
$fields{"Phone"} = "\n\nPhone"; 
$fields{"Cell_Phone"} = "\n\nCell Phone"; 
$fields{"Email"} = "\n\nEmail"; 
$fields{"School"} = "\n\nSchool"; 
$fields{"Grade_Level"} = "\n\nCurrent Grade Level"; 
$fields{"Volunteer_Opportunities"} = "\n\nVolunteer Opportunities"; 
$fields{"T-Shirt"} = "\n\nT-Shirt";
$fields{"Message"} = "\n\nMessage"; 
 
$body = "Jesus and You: Youth Volunteer Application:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 
 
$headers2 = "From: elizabethiannarino@gmail.com"; 
$subject2 = "Thank you for submitting your application."; 
$autoreply = "Your application has been received.  Please contact us at 610.696.7466 if you have any additional questions. 
 
Somebody will get back to you as soon as possible.";
 
 
if($from == '') {print "You have not entered an email, please go back and try again";} 
else { 
if($name == '') {print "You have not entered a name, please go back and try again";} 
else { 
$send = mail($to, $subject, $body, $headers); 
$send2 = mail($from, $subject2, $autoreply, $headers2); 
if($send) 
{header("Location: http://www.bcwc.org/happenings/thanks.html");} 
else 
{print "We encountered an error sending your mail, please notify elizabethiannarino@gmail.com";} 
}
}
?>
Can anyone help???

Re: PHP Form Help with Check Boxes

Posted: Thu Jan 31, 2008 11:34 am
by JAM
I dont see any checkboxes? :dubious:

Re: PHP Form Help with Check Boxes

Posted: Thu Jan 31, 2008 12:18 pm
by eiannarino
Sorry, I put up the wrong form, I reposted the form with the checkboxes above!

Thanks!

Re: PHP Form Help with Check Boxes

Posted: Thu Jan 31, 2008 12:27 pm
by JAM
For checkboxes use them this way;

Code: Select all

<input type="checkbox" name="Volunteer_Opportunities[]" value="Drama/skit Actor" />
<input type="checkbox" name="Volunteer_Opportunities[]" value="5 Year Olds Teacher's Assistant" />
On the contactyouth.php page, at the very top, add this:

Code: Select all

<pre>
<?php
    print_r($_POST);
    exit;
?>
Then rerun the form and see what happens. This last part is just to give you the idea of what happens, that the $_POST['Volunteer_Opportunities'] becomes an array with the selected values within.
Hope it helped.

Re: PHP Form Help with Check Boxes

Posted: Thu Jan 31, 2008 12:40 pm
by eiannarino
It did not work. The contactyouth.php processing page came up and showed this:

Code: Select all

Array
(
    [sendto] => elizabethiannarino@gmail.com
    [Name] => test
    [Street_Address] => test
    [City] => test
    [State] => test
    [Zipcode] => test
    [Phone] => test
    [Cell_Phone] => test
    [Email] => test
    [School] => test
    [Grade_Level] => test
    [Volunteer_Opportunities] => Array
        (
            [0] => Craft Room Helper
            [1] => Drama/skit Actor
            [2] => Preschool Teacher\'s Assistant
            [3] => 5 Year Olds Teacher\'s Assistant
            [4] => Kindergarten Teacher\'s  Assistant
            [5] => 1st Grade Teacher\'s Assistant
            [6] => 2nd Grade  Teacher\'s Assistant
            [7] => 3rd Grade  Teacher\'s Assistant
            [8] => 4th Grade Teacher\'s  Assistant
            [9] => 5th Grade  Teacher\'s Assistant
        )
 
    [T-Shirt] => Child Small
    [Message] => test
    [send] => Submit
)
And it never went to the thankyou page and it never got sent to my email.

Re: PHP Form Help with Check Boxes

Posted: Thu Jan 31, 2008 12:48 pm
by JAM
This is because of the code I posted.
The print_r() prints the info being sendt from the form, the exit; from stop the mail from being sendt. This because I wanted you to see whats happen.

As you see, $_POST['Volunteer_Opportunities'] is now and array. You should apply, for example, a foreach loop to that to get the actual values. Example code here:

Code: Select all

$data = '';
foreach ($_POST['Volunteer_Opportunities'] as $k => $v) {
    $data .= $v;
}

Re: PHP Form Help with Check Boxes

Posted: Thu Jan 31, 2008 1:04 pm
by eiannarino
I am new to this and am not sure where to put that line of code you posted? Does something have to change in the code you posted?

Re: PHP Form Help with Check Boxes

Posted: Thu Jan 31, 2008 1:19 pm
by JAM
I don't like posting finished code so I hope youre learning while typing here...

Remnove the print_r() and exit code I posted earlier. Change your script that sais;

Code: Select all

$volunteer = $_REQUEST['Volunteer_Opportunities'] ;
...to...

Code: Select all

 
    $volunteer = '';
    foreach ($_REQUEST['Volunteer_Opportunities'] as $k => $v) {
        $volunteer .= $v;
    }
...and try running the script.

Perhaps it works a bit more as wanted now.

Re: PHP Form Help with Check Boxes

Posted: Thu Jan 31, 2008 1:25 pm
by eiannarino
That is what I did and it didn't work before so I was seeing if you did it differently. I get an error:


Notice: Array to string conversion in D:\SITES\bcwc.org\happenings\contactyouth.php on line 36

Re: PHP Form Help with Check Boxes

Posted: Thu Jan 31, 2008 1:36 pm
by JAM
What does lines 30 - 40 look like?

Re: PHP Form Help with Check Boxes

Posted: Thu Jan 31, 2008 1:40 pm
by eiannarino

Code: Select all

<?php 
$to = $_REQUEST['sendto'] ; 
$name = $_REQUEST['Name'] ;
$address = $_REQUEST['Street_Address'] ; 
$city = $_REQUEST['City'] ; 
$state = $_REQUEST['State'] ; 
$zipcode = $_REQUEST['Zipcode'] ; 
$phone = $_REQUEST['Phone'] ; 
$cellphone = $_REQUEST['Cell_Phone'] ; 
$from = $_REQUEST['Email'] ; 
$school = $_REQUEST['School'] ; 
$grade = $_REQUEST['Grade_Level'] ; 
$volunteer = '';
    foreach ($_REQUEST['Volunteer_Opportunities'] as $k => $v) {
        $volunteer .= $v;
    }
$tshirt = $_REQUEST['T-Shirt'] ;   
$headers = "From: $from"; 
$subject = "Jesus and You: Youth Volunteer Application"; 
 
$fields = array(); 
$fields{"Name"} = "\n\nName"; 
$fields{"Street_Address"} = "\n\nStreet Address"; 
$fields{"City"} = "\n\nCity"; 
$fields{"State"} = "\n\nState"; 
$fields{"Zipcode"} = "\n\nZip Code"; 
$fields{"Phone"} = "\n\nPhone"; 
$fields{"Cell_Phone"} = "\n\nCell Phone"; 
$fields{"Email"} = "\n\nEmail"; 
$fields{"School"} = "\n\nSchool"; 
$fields{"Grade_Level"} = "\n\nCurrent Grade Level"; 
$fields{"Volunteer_Opportunities"} = "\n\nVolunteer Opportunities"; 
$fields{"T-Shirt"} = "\n\nT-Shirt";
$fields{"Message"} = "\n\nMessage"; 
 
$body = "Jesus and You: Youth Volunteer Application:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 
 
$headers2 = "From: elizabethiannarino@gmail.com"; 
$subject2 = "Thank you for submitting your application."; 
$autoreply = "Your application has been received.  Please contact us at 610.696.7466 if you have any additional questions. 
 
Somebody will get back to you as soon as possible.";
 
 
if($from == '') {print "You have not entered an email, please go back and try again";} 
else { 
if($name == '') {print "You have not entered a name, please go back and try again";} 
else { 
$send = mail($to, $subject, $body, $headers); 
$send2 = mail($from, $subject2, $autoreply, $headers2); 
if($send) 
{header("Location: http://www.bcwc.org/happenings/thanks.html");} 
else 
{print "We encountered an error sending your mail, please notify elizabethiannarino@gmail.com";} 
}
}
?>

Re: PHP Form Help with Check Boxes

Posted: Thu Jan 31, 2008 1:57 pm
by JAM
Change:

Code: Select all

$body = "Jesus and You: Youth Volunteer Application:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
...to something like this...

Code: Select all

    $body = "Jesus and You: Youth Volunteer Application:\n\n"; 
    foreach($fields as $a => $b) { 
        if ($a == 'Volunteer_Opportunities') {
            $body .= ("Volunteer_Opportunities: $volunteer\n",$b,$_REQUEST[$a]); 
        } else {
            $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); 
        }
    }
The overall code looks abit wierd. It's setting a bunch of varaibles without using them (?). You should likely also change the...

Code: Select all

$volunteer .= $v;
...to...

Code: Select all

$volunteer .= $v.', ';
...so that you add commas between the values.

Re: PHP Form Help with Check Boxes

Posted: Thu Jan 31, 2008 3:58 pm
by eiannarino
Thanks for you help, it is getting a parsing error.


Parse error: parse error, unexpected ',' in D:\SITES\bcwc.org\happenings\contactyouth.php on line 39

Code: Select all

<?php 
$to = $_REQUEST['sendto'] ; 
$name = $_REQUEST['Name'] ;
$address = $_REQUEST['Street_Address'] ; 
$city = $_REQUEST['City'] ; 
$state = $_REQUEST['State'] ; 
$zipcode = $_REQUEST['Zipcode'] ; 
$phone = $_REQUEST['Phone'] ; 
$cellphone = $_REQUEST['Cell_Phone'] ; 
$from = $_REQUEST['Email'] ; 
$school = $_REQUEST['School'] ; 
$grade = $_REQUEST['Grade_Level'] ; 
$volunteer = '';
    foreach ($_REQUEST['Volunteer_Opportunities'] as $k => $v) {
        $volunteer .= $v;
    }
$tshirt = $_REQUEST['T-Shirt'] ;   
$headers = "From: $from"; 
$subject = "Jesus and You: Youth Volunteer Application"; 
 
$fields = array(); 
$fields{"Name"} = "\n\nName"; 
$fields{"Street_Address"} = "\n\nStreet Address"; 
$fields{"City"} = "\n\nCity"; 
$fields{"State"} = "\n\nState"; 
$fields{"Zipcode"} = "\n\nZip Code"; 
$fields{"Phone"} = "\n\nPhone"; 
$fields{"Cell_Phone"} = "\n\nCell Phone"; 
$fields{"Email"} = "\n\nEmail"; 
$fields{"School"} = "\n\nSchool"; 
$fields{"Grade_Level"} = "\n\nCurrent Grade Level"; 
$fields{"Volunteer_Opportunities"} = "\n\nVolunteer Opportunities"; 
$fields{"T-Shirt"} = "\n\nT-Shirt";
$fields{"Message"} = "\n\nMessage"; 
 
$body = "Jesus and You: Youth Volunteer Application:\n\n"; 
    foreach($fields as $a => $b) { 
        if ($a = 'Volunteer_Opportunities') {
            $body .= ("Volunteer_Opportunities: $volunteer\n",$b,$_REQUEST[$a]); 
        } else {
            $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); 
        }
    }
 
 
$headers2 = "From: elizabethiannarino@gmail.com"; 
$subject2 = "Thank you for submitting your application."; 
$autoreply = "Your application has been received.  Please contact us at 610.696.7466 if you have any additional questions. 
 
Somebody will get back to you as soon as possible.";
 
 
if($from == '') {print "You have not entered an email, please go back and try again";} 
else { 
if($name == '') {print "You have not entered a name, please go back and try again";} 
else { 
$send = mail($to, $subject, $body, $headers); 
$send2 = mail($from, $subject2, $autoreply, $headers2); 
if($send) 
{header("Location: http://www.bcwc.org/happenings/thanks.html");} 
else 
{print "We encountered an error sending your mail, please notify elizabethiannarino@gmail.com";} 
}
}
?>