hey guys, im a bit new to php. I was wondering if any of you guys could help me out with this email code i have to send me an email. I have a website with a service link then the user needs to pick which kind of service he/she requires. Everything works except the checkbox part. It sends me an email and it's blank there. Here is my code:

<?
include("../header.php"); 


$name= $_POST['name'];
$from = $_POST["email"];
$to = "je@example.com";
$subject = "Proposal Request";
$message = "Company Name: " . $_POST["companyname"] . "\n";
$message .= "Telephone Number:" . $_POST["telephonenumber"] . "\n";
$message .= "Email Address:" . $_POST["email"] . "\n";
$message .= "Company Name:" . $_POST["companyname"] . "\n";
$message .= "Company Address:" . $_POST["location"] . "\n";
$message .= "City:" . $_POST["city"] . "\n";
$message .= "State:" . $_POST["state"] . "\n";
[B]$message .= "Type of Service needed:" . $_POST["service[]"] . "\n";[/B]
$message .= "Best Time to Contact:" . $_POST["time[]"] . "\n";
$message .= "Contact by Email/Phone:" . $_POST["cpref[]"] . "\n";


if($sent){
	$headers = 'From: ' . $name . ' <' . $from . '>' . "\r\n" .
		'Reply-To: ' . $from . "\r\n" .
		'X-Mailer: PHP/' . phpversion();
	
	mail($to, $subject, $message, $headers);
	echo "The Email Has Been Sent";
	

}
?>

<div style="position: absolute; left:149px; top:320px;">

<table bgcolor="#ffffff" width="800" border="0">
<tr>
<td>
<div align="center">
  <p>&nbsp;</p>
  <p><img src="proposalssign.jpg" width="92" height="29" />
    </p>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table width="680" border="0" align="center">
  <tr>
    <td>Need a Free Quote for your company? Not a problem just fill out the indicated boxes below, and we'll be sure to give you a call within the next 24 hours. </td>
  </tr>
</table>
<p>
 
 
  <!-- Begin Proposal Form -->


<form enctype="multipart/form-data" method="post" action="proposal.php" accept-charset="UTF-8">
      	<input type="hidden" name="sent" value="1" />
	<table cellspacing="5" cellpadding="5" border="0">
		<tr>
			<td valign="top">
				<strong>Name:</strong>
			</td>
			<td valign="top">
				<input type="text" name="name" id="name" size="40" value="" />
				
			</td>
		</tr>
		<tr>
			<td valign="top">
				<strong>Telephone Number:</strong>
			</td>
			<td valign="top">
				<input type="text" name="telephonenumber" id="telephonenumber" size="40" value="" />
				
			</td>
		</tr>
		<tr>
			<td valign="top">
				<strong>Email Address:</strong>
			</td>
			<td valign="top">
				<input type="text" name="email" id="email" size="40" value="" />
				
			</td>
		</tr>
		<tr>
			<td valign="top">
				<strong>Company Name:</strong>
			</td>
			<td valign="top">
				<input type="text" name="companyname" id="companyname" size="40" value="" />
				
			</td>
		</tr>
		<tr>
			<td valign="top">
				<strong>Company Address:</strong>
			</td>
			<td valign="top">
				<input type="text" name="location" id="location" size="40" value="" />
				
			</td>
		</tr>
		<tr>
			<td valign="top">
				<strong>City:</strong>
			</td>
			<td valign="top">
				<input type="text" name="city" id="city" size="40" value="" />
				
			</td>
		</tr>
		<tr>
			<td valign="top">
				<strong>State:</strong>
			</td>
			<td valign="top">
				<input type="text" name="state" id="state" size="40" value="" />
				
			</td>
		</tr>
		<tr>
			<td valign="top">
				<strong>What kind of service are you looking for?</strong>
			</td>
			<td valign="top">
				<input type="checkbox" name="service[]" id="service_0" value="One-Time Scrub Down" /> One-Time Scrub Down<br/>
				<input type="checkbox" name="service[]" id="service_1" value="Daily Maintenance" /> Daily Maintenance<br/>
				<input type="checkbox" name="service[]" id="service_2" value="Weekend Maintenance" /> Weekend Maintenance<br/>
				
			</td>
		</tr>
		<tr>
			<td valign="top">
				<strong>What&#39;s the best time to contact you at?</strong>
			</td>
			<td valign="top">
				<input type="checkbox" name="time[]" id="time_0" value="Morning" />Morning<br/>
				<input type="checkbox" name="time[]" id="time_1" value="Afternoon" />Afternoon<br/>
                <input type="checkbox" name="time[]" id="time_2" value="Evening" />Evening<br/>
				
			</td>
		</tr>
        
        <tr>
			<td valign="top">
				<strong>What is the preferred method to contact you?</strong>
			</td>
			<td valign="top">
				<input type="checkbox" name="cpref[]" id="cpref_0" value="Email" />Email<br/>
				<input type="checkbox" name="cpref[]" id="cpref_1" value="Phone" />Phone<br/>
               
				
			</td>
		</tr>
        
		<tr>
			<td colspan="2" align="center">
				<input type="submit" value=" Submit Form " />
			</td>
		</tr>
	</table>
</form>




<!-- End Proposal Form --><br /><br />

Recommended Answers

All 14 Replies

$service = $_POST['service'];

if(is_array($service))
{
	$comma = "";
	$service_type = "";
	
	foreach($service as $value)
	{
		$service_type .= $comma.$value;
		$comma = ", ";
	}
	
	$message .= "Type of Service needed:" .$service_type . "\n";
}

Same for other check boxes...

Looks like manish.s beat me too it. Just remember that your checkbox group is an array, and they have to be extracted with a loop. Also, a radio button may be better for the service if you only want one possible value.

Optimized code...

$service = $_POST['service'];

if(is_array($service))
{
    $message .= "Type of Service needed:" .implode(", ",$service). "\n";
}

thank you so much guys. You are the best :) got another question...is there anyway i can direct them to a "thank you page" instead of echoing a message?

try this...

$mail_sent = mail($to, $subject, $message, $headers);

if($mail_sent)
{
	header("location:thankyou.php");
}

ok i implimented that into the code i had already, but it didn't work. It gave me an error. This is what i did:

if($sent){
	$headers = 'From: ' . $name . ' <' . $from . '>' . "\r\n" .
		'Reply-To: ' . $from . "\r\n" .
		'X-Mailer: PHP/' . phpversion();
	
	mail($to, $subject, $message, $headers);
	echo "The Email Has Been Sent";
        [B]header("location:thankyou.php");[/B]

}

btw what does the X-Mailer thing mean? Thanks again guys!

Remove echo "The Email Has Been Sent"; . The header has to be the first thing sent to the browser.

It gives me this error:


Warning: Cannot modify header information - headers already sent by (output started at /home/jebgroup/public_html/proposal.php:6) in /home/jebgroup/public_html/proposal.php on line 66

Yes, you can't echo or print anything prior to using header. Headers can only be sent once. If you have any output to the browser, headers are sent. Then when you try to send more headers, you get errors.

i took off the echo though and replaced that with the code you gave me. What i have on my website is basically the layout and the content. The layout is in a header.php file, and the content are in the other .php's every page such as proposal .php includes the header(layout). Is there any other way i can do this then? Here is a better explanation of what i said.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
<!--
.style2 {font-size: 14px; }
-->
</style>

</head>

<body>


[B]<?
include("../header.php"); //This includes the layout [/B]


$name= $_POST['name'];
$from = $_POST["email"];
$to = "blah@blah.com";
$subject = "Proposal Request";
$message = "Company Name: " . $_POST["companyname"] . "\n";
$message .= "Telephone Number:" . $_POST["telephonenumber"] . "\n";
$message .= "Email Address:" . $_POST["email"] . "\n";
$message .= "Company Name:" . $_POST["companyname"] . "\n";
$message .= "Company Address:" . $_POST["location"] . "\n";
$message .= "City:" . $_POST["city"] . "\n";
$message .= "State:" . $_POST["state"] . "\n";

//Checkboxes
//Service Needed
$service = $_POST['service'];
 if(is_array($service))
{
    $message .= "Type of Service needed:" .implode(", ",$service). "\n";
}

//Best Time to Contact
$time = $_POST['time'];
 if(is_array($time))
{
    $message .= "Best Time to Contact:"  .implode(", ",$time). "\n";
}




//Contact Preference
$cpref = $_POST['cpref'];
 if(is_array($cpref))
{
    $message .= "Contact by Email/Phone:"  .implode(", ",$cpref). "\n";
}



if($sent){
	$headers = 'From: ' . $name . ' <' . $from . '>' . "\r\n" .
		'Reply-To: ' . $from . "\r\n" .
		'X-Mailer: PHP/' . phpversion();
	
	mail($to, $subject, $message, $headers);
	//echo "The Email Has Been Sent";
	[B]header("location:thankyou.php"); //that gives me the error[/B]
        

	

}
?>

Is there anyway i can do this?

Yes, delete everything before the <? delimiter and use the script as a cgi. Since you are doing a redirect, you don't need to send any content to the browser. Just the header.

you mean delete all the html headers and stuff? and change the extension from header.php to header.cgi?

If still u are getting error try this code in place of header("location:thankyou.php");

echo "<script language='JavaScript'>
		location.href='thankyou.php';
	  </script>";

you mean delete all the html headers and stuff? and change the extension from header.php to header.cgi?

No don't change the .php extension. Here's how I would do it:

<?php
$name= $_POST['name'];
$from = $_POST["email"];
$to = "blah@blah.com";
$subject = "Proposal Request";
$message = "Company Name: " . $_POST["companyname"] . "\n";
$message .= "Telephone Number:" . $_POST["telephonenumber"] . "\n";
$message .= "Email Address:" . $_POST["email"] . "\n";
$message .= "Company Name:" . $_POST["companyname"] . "\n";
$message .= "Company Address:" . $_POST["location"] . "\n";
$message .= "City:" . $_POST["city"] . "\n";
$message .= "State:" . $_POST["state"] . "\n";

//Checkboxes
//Service Needed
$service = $_POST['service'];
 if(is_array($service))
{
    $message .= "Type of Service needed:" .implode(", ",$service). "\n";
}

//Best Time to Contact
$time = $_POST['time'];
 if(is_array($time))
{
    $message .= "Best Time to Contact:"  .implode(", ",$time). "\n";
}
//Contact Preference
$cpref = $_POST['cpref'];
 if(is_array($cpref))
{
    $message .= "Contact by Email/Phone:"  .implode(", ",$cpref). "\n";
}
if($sent){
	$headers = 'From: ' . $name . ' <' . $from . '>' . "\r\n" .
		'Reply-To: ' . $from . "\r\n" .
		'X-Mailer: PHP/' . phpversion();
	
	mail($to, $subject, $message, $headers);
	header("location:thankyou.php"); 
        exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
<!--
.style2 {font-size: 14px; }
-->
</style>

</head>

<body>


<?php
include("../header.php"); //This includes the layout 
?>

Notice how most of the server side processing is done before the doctype. Also, the header is sent prior to any html output.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.