Page 2 of 2

Needs Help With Retrieving Check Box Results

Posted: Fri Oct 10, 2008 8:26 pm
by Misty
I have it working now, but I need help with getting the results for the check boxes.

You can see my working code for Contact.PHP now:

Code: Select all

 
 
<?php //SET HEADERS TO ACCEPT A HTML MAIL
 
$Name = $_POST['txtName']; 
$from_email = $_POST['txtEmail']; 
 
 
//SET EMAIL VARIABLES
            $to = "misty@you.com";
            $subject = "Contact Form From Stello Home Services";
            $message ="<b>Name:</b>&nbsp;".$_POST["txtName"]."<p/>";
            $message .="<b>Email:</b>&nbsp;".$_POST["txtEmail"]."<p/>";
            $message .="<b>Mailing Address:</b>&nbsp;".$_POST["txtAddress"]."<p/>";
            $message .="<b>City, State, and Zip Code:</b>&nbsp;".$_POST["txtCity"].", ".$_POST["SelState"]." ".$_POST["txtZip"]."<p/>";
            $message .="<b>Phone:</b>&nbsp;".$_POST["txtPhone"]."<p/>";
            $message .="<b>Project Type(s):</b>&nbsp;".$_POST["txtaProjectType"]."<p/>";
            
            $from = $from_email; //set this to the "from email"
        
        //SET EMAIL HEADERS
            $headers = "MIME-Version: 1.0\r\n";
            $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
            $headers .= "Content-Transfer-Encoding: 7bit\r\n";
            $headers .= "From: " . $Name . "\r\n";
 
 
 
//YOUR MESSAGE CAN BE EMBEDDED WITH THE FIELDS U RECEIVE FROM FORM SUBMISSION
 
//SEND EMAIL
 $sent = mail($to,$subject,$message,$headers,"-f $from")
 
?>
 
 
How would I go about getting the results for the check boxes? There could be more than one choice. Let me share the html code for the check boxes below.

Code: Select all

 
 
      <tr bgcolor="#e8e8e8"> 
                            <td height="27" align=left><font size="-1" face="arial">Type 
                              of Project </font></td>
                            <td valign="top"><input type="checkbox" name="checkbox1" value="NewConstruction">
                              <strong>New Construction &nbsp; 
                              <input type="checkbox" name="checkbox2" value="Renovation">
                              Renovation &nbsp; 
                              <input type="checkbox" name="checkbox3" value="Addition">
                              Addition<br>
                              <br>
                              <input type="checkbox" name="checkbox4" value="Bathroom">
                              Bathroom &nbsp; 
                              <input type="checkbox" name="checkbox5" value="Kitchen">
                              Kitchen &nbsp; 
                              <input type="checkbox" name="checkbox6" value="Other">
                              Other </strong></td>
                          </tr>
 
 

Re: Needs Help With PHP Web Page For Form

Posted: Sat Oct 11, 2008 6:46 am
by aceconcepts
If you name all your checkboxes the same like this:

Code: Select all

 
<tr bgcolor="#e8e8e8">
                            <td height="27" align=left><font size="-1" face="arial">Type
                              of Project </font></td>
                            <td valign="top"><input type="checkbox" name="checkbox1" value="NewConstruction">
                              <strong>New Construction &nbsp;
                              <input type="checkbox" name="chkbox[]" value="Renovation">
                              Renovation &nbsp;
                              <input type="checkbox" name="chkbox[]" value="Addition">
                              Addition<br>
                              <br>
                              <input type="checkbox" name="chkbox[]" value="Bathroom">
                              Bathroom &nbsp;
                              <input type="checkbox" name="chkbox[]" value="Kitchen">
                              Kitchen &nbsp;
                              <input type="checkbox" name="chkbox[]" value="Other">
                              Other </strong></td>
                          </tr>
 
Notice the "[]" after the name "chkbox". This will pass "chkbox" as an array.

This is how you get the selected checkboxes:

Code: Select all

 
//assume "kitchen" and "bathroom" have been checked
foreach($_POST['chkbox'] as $value)
{
 //$value is the value passed from the checkboxes
 echo $value .'<br />'; //this will output "bathroom" and "kitchen" on a new line
}
 
Hope it makes sense :D

Re: Needs Help With PHP Web Page For Form

Posted: Sat Oct 11, 2008 12:47 pm
by Misty
I have gotten the check boxes figured out. But I need help with displaying the results in the email. I did it without using the $message and it showed the results on the web page. This is not what I wanted. I want it to show up on the email the misty@you.com receives. I tried doing it, but I got the following error message:

Parse error: syntax error, unexpected T_FOREACH in /home/stelloho/public_html/contact.php on line 17

Here's the code (Please look at the bolded parts):

Code: Select all

 
 
<?php //SET HEADERS TO ACCEPT A HTML MAIL
 
$Name = $_POST['txtName']; 
$from_email = $_POST['txtEmail']; 
 
 
//SET EMAIL VARIABLES
            $to = "misty@you.com";
            $subject = "Contact Form From Stello Home Services";
            $message ="<b>Name:</b>&nbsp;".$_POST["txtName"]."<p/>";
            $message .="<b>Email:</b>&nbsp;".$_POST["txtEmail"]."<p/>";
            $message .="<b>Mailing Address:</b>&nbsp;".$_POST["txtAddress"]."<p/>";
            $message .="<b>City, State, and Zip Code:</b>&nbsp;".$_POST["txtCity"].", ".$_POST["SelState"]." ".$_POST["txtZip"]."<p/>";
            $message .="<b>Phone:</b>&nbsp;".$_POST["txtPhone"]."<p/>";
            //assume "kitchen" and "bathroom" have been checked
[b]$message .="<b>Project Type(s):</b>&nbsp;" 
.foreach($_POST['chkbox'] as $value) - [b](where the error message originates from)[/b]{
 //$value is the value passed from the checkboxes
 echo $value .'<br />'; //this will output "bathroom" and "kitchen" on a new line
}[/b]           $message .="<b>Details About Project:</b>&nbsp;".$_POST["txtaProjectType"]."<p/>";
            
            $from = $from_email; //set this to the "from email"
        
        //SET EMAIL HEADERS
            $headers = "MIME-Version: 1.0\r\n";
            $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
            $headers .= "Content-Transfer-Encoding: 7bit\r\n";
            $headers .= "From: " . $Name . "\r\n";
 
 
 
//YOUR MESSAGE CAN BE EMBEDDED WITH THE FIELDS U RECEIVE FROM FORM SUBMISSION
 
//SEND EMAIL
 $sent = mail($to,$subject,$message,$headers,"-f $from")
 
?>
 
 

Re: Needs Help With PHP Web Page For Form

Posted: Sat Oct 11, 2008 1:06 pm
by aceconcepts
You placed the foreach loop in the middle of assigning values to a variable.

I've amended your code:

Code: Select all

 
<?php //SET HEADERS TO ACCEPT A HTML MAIL
 
$Name = $_POST['txtName'];
$from_email = $_POST['txtEmail'];
 
 
//SET EMAIL VARIABLES
            $to = "misty@you.com";
            $subject = "Contact Form From Stello Home Services";
            $message ="<b>Name:</b>&nbsp;".$_POST["txtName"]."<p/>";
            $message .="<b>Email:</b>&nbsp;".$_POST["txtEmail"]."<p/>";
            $message .="<b>Mailing Address:</b>&nbsp;".$_POST["txtAddress"]."<p/>";
            $message .="<b>City, State, and Zip Code:</b>&nbsp;".$_POST["txtCity"].", ".$_POST["SelState"]." ".$_POST["txtZip"]."<p/>";
            $message .="<b>Phone:</b>&nbsp;".$_POST["txtPhone"]."<p/>";
            //assume "kitchen" and "bathroom" have been checked
$message .="<b>Project Type(s):</b>&nbsp;";
foreach($_POST['chkbox'] as $value)
{
 //$value is the value passed from the checkboxes
 $message.=$value .'<br />';
}
$message .="<b>Details About Project:</b>&nbsp;".$_POST["txtaProjectType"]."<p/>";
           
            $from = $from_email; //set this to the "from email"
       
        //SET EMAIL HEADERS
            $headers = "MIME-Version: 1.0\r\n";
            $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
            $headers .= "Content-Transfer-Encoding: 7bit\r\n";
            $headers .= "From: " . $Name . "\r\n";
 
 
 
//YOUR MESSAGE CAN BE EMBEDDED WITH THE FIELDS U RECEIVE FROM FORM SUBMISSION
 
//SEND EMAIL
 $sent = mail($to,$subject,$message,$headers,"-f $from")
 
?>
 

Re: Needs Help With PHP Web Page For Form

Posted: Sat Oct 11, 2008 1:38 pm
by Misty
Thank you for your help! There is one more thing I need to do. I would like for contact.php page to have a message telling the user if their message was successfully sent and some information. How do I do that? I tried an if statement before, but it didn't work. I am very new to PHP so I am still learning a lot.

Re: Needs Help With PHP Web Page For Form

Posted: Sat Oct 11, 2008 2:26 pm
by aceconcepts
This would be done when you use mail():

Code: Select all

 
if(mail($to,$subject,$message,$headers,"-f $from"))
{
//mail sent - WOOHOO
}
else
{
//mail not sent - BOOHOO
}
 

Re: Needs Help With PHP Web Page For Form

Posted: Sat Oct 11, 2008 2:57 pm
by Misty
aceconcepts wrote:This would be done when you use mail():

Code: Select all

 
if(mail($to,$subject,$message,$headers,"-f $from"))
{
//mail sent - WOOHOO
}
else
{
//mail not sent - BOOHOO
}
 
This didn't work. I got the following error message:

Parse error: syntax error, unexpected T_IF in /home/stelloho/public_html/contact.php on line 39. This refers to the first line.

Re: Needs Help With PHP Web Page For Form

Posted: Sat Oct 11, 2008 6:02 pm
by aceconcepts
Can you post your code so i can see how it looks?

Re: Needs Help With PHP Web Page For Form

Posted: Sat Oct 11, 2008 7:54 pm
by Misty
aceconcepts wrote:Can you post your code so i can see how it looks?
Here's my code:

Code: Select all

 
<?php //SET HEADERS TO ACCEPT A HTML MAIL
 
$Name = $_POST['txtName'];
$from_email = $_POST['txtEmail'];
 
 
//SET EMAIL VARIABLES
            $to = "misty@you.com";
            $subject = "Contact Form From Stello Home Services";
            $message ="<b>Name:</b>&nbsp;".$_POST["txtName"]."<p/>";
            $message .="<b>Email:</b>&nbsp;".$_POST["txtEmail"]."<p/>";
            $message .="<b>Mailing Address:</b>&nbsp;".$_POST["txtAddress"]."<p/>";
            $message .="<b>City, State, and Zip Code:</b>&nbsp;".$_POST["txtCity"].", ".$_POST["SelState"]." ".$_POST["txtZip"]."<p/>";
            $message .="<b>Phone:</b>&nbsp;".$_POST["txtPhone"]."<p/>";
            //assume "kitchen" and "bathroom" have been checked
$message .="<b>Project Type(s):</b><br>";
foreach($_POST['chkbox'] as $value)
{
 //$value is the value passed from the checkboxes
 $message.=$value .'<br/>';
}
$message .="<p><b>Details About Project:</b>&nbsp;".$_POST["txtaProjectType"]."<p/>";
           
            $from = $from_email; //set this to the "from email"
       
        //SET EMAIL HEADERS
            $headers = "MIME-Version: 1.0\r\n";
            $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
            $headers .= "Content-Transfer-Encoding: 7bit\r\n";
            $headers .= "From: " . $Name . "\r\n";
 
 
 
//YOUR MESSAGE CAN BE EMBEDDED WITH THE FIELDS U RECEIVE FROM FORM SUBMISSION
 
//SEND EMAIL
 $sent = mail($to,$subject,$message,$headers,"-f $from")
 
 if(mail($to,$subject,$message,$headers,"-f $from"))
{
Your information has been successfully sent to Stello Home Services. We will contact you shortly. 
}
else
{
Your information was unsuccessfully sent. Please try again! 
}
 
?>
 

Re: Needs Help With PHP Web Page For Form

Posted: Sat Oct 11, 2008 8:05 pm
by aceconcepts
From you code above, remove line 38 or comment it out using "//" at the beginning.

Also, remember to echo a message such as on lines 42 and 46.

Re: Needs Help With PHP Web Page For Form

Posted: Sat Oct 11, 2008 8:09 pm
by Misty
aceconcepts wrote:From you code above, remove line 38 or comment it out using "//" at the beginning.

Also, remember to echo a message such as on lines 42 and 46.
I don't understand. if we removed the line of code below:

Line 38
$sent = mail($to,$subject,$message,$headers,"-f $from")

then it would not send out emails any more.

Re: Needs Help With PHP Web Page For Form

Posted: Sun Oct 12, 2008 6:00 am
by aceconcepts
It will if all values are valid and mail() is able to send it, because you have stated it in the IF statement at the bottom.