Topic: CC option for contact form.

Hey gang,

I'm working on a contact / work inquiry form for my site and want to add a "CC me a copy" to the form and use the value the client gives as his or her email.

However, I'm a php idiot and the contact form tutorials I am teaching myself from don't include anything like what I'm looking for.

How would I add that?

Thanks!

Lorem ipsum dolor sit amet, consetetur sadipscing elitr.

Re: CC option for contact form.

$to = 'me@example.com';
$subject = 'the subject of the email';
$message = 'this is the message or body of the email...';
$headers = 'Cc: ccaddress@example.com';

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

check out http://us3.php.net/function.mail for more info

Last edited by dmarino (2008-08-09 16:27:11)

Daniel Marino | www.iamdanielmarino.com

Re: CC option for contact form.

GRAR! HULK HATE PHP! HULK SMASH SILLY PROGRAMMING CODE!

Can someone who actually knows what he or she is doing with PHP help me out? I've frankensteined this code together from a couple different tutorials, and I'm getting nothing.

It's all on display at http://willphillipsdesign.com/v3/

The Forum:

	<form method="POST" action="mailer.php">
	<fieldset>

	<ul>
		<li><label>Name: <strong>*</strong></label> <input type="text" name="name" size="30" />
		</li>
		<li><label>Email: <strong>*</strong></label> <input type="text" name="email" size="30" />
		</li>
		<li><label>Email me a CC:</label><input type="checkbox" name="cc" value="Yes" class="checkbox">
		</li>
		<li><label>Phone:</label> <input type="text" name="phone" size="30" />
		</li>
		<li><label>Company:</label> <input type="text" name="company" size="30" />
		</li>
		<li><label>Project Type:</label>
			<select name="type">
				<option value=""></option>
				<option value="project-web">Website Design or Maintenance</option>
				<option value="project-print">Print Media or Other Graphic Design</option>
				<option value="project-">Branding and Logo Development</option>
				<option value="project-">Copywriting</option>
				<option value="project-complicated">It's Complicated</option>
			</select>
		</li>
		<li><label>Tell me about your project. <strong>*</strong></label><textarea name="project-info" cols="28" rows="10"></textarea>
		</li>
		<li><label>Who is your target market?</label><textarea name="market" cols="28" rows="10"></textarea>
		</li>
		<li><label>What are your goals with this project?</label><textarea name="goals" cols="28" rows="10"></textarea>
		</li>
		<li><label>Estimated Budget:</label>
			<select name="budget">
				<option value=""></option>
				<option value="budget-500-1500">$500 - $1500</option>
				<option value="budget-1500-2500">$1500 - $2500</option>
				<option value="budget-200-5000">$2500 - $5000</option>
				<option value="budget-5000+">$5000+</option>

			</select>
		</li>
		<li><label>Timeframe:</label>
			<select name="timeframe">
				<option value=""></option>
				<option value="time-asap">ASAP</option>
				<option value="time-2_months">2 Months</option>
				<option value="time-6_months">6 Months</option>
				<option value="time-flexible">I'm Flexible</option>
			</select>
		</li>
		<li><label>How did you find me?</label> <input type="text" name="find-me" size="30" />
		</li>
		<li class="last"><label></label>
		<button id="submit" name="submit" type="submit"><img src="img/site/submit.png" border="0" alt="">Submit</button>
		<button id="clear" class="right" name="clear" type="submit"><img src="img/site/clear.png" border="0" alt="">Clear</button>
		</li>
	</ul>
	</fieldset>
	</form>

Mailer.php:

<?php
if(isset($_POST['submit'])) {


// Pick up the form data and assign it to variables
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$company = $_POST['company'];
$type = $_POST['type'];
$project-info = $_POST['project-info'];
$market = $_POST['market'];
$goals = $_POST['goals'];
$budget = $_POST['budget'];
$timeframe = $_POST['timeframe'];
$find-me = $_POST['find-me'];


// Build the email (replace the address in the $to section with your own)
$to = 'willphillipsdesign@gmail.com';
$subject = "WillPhillipsDesign.com Contact";

$headers = "From: $email";
$headers = "Cc: $email\n"; 
$headers = "MIME-Version: 1.0\n";
$headers = "Content-type: text/html; charset=iso-8859-1\n";

$message = "<strong>{$name} <br />{$company}</strong> <br />{$email} <br />{$phone} <br /><br /><strong>{$type}</strong> <br />{$market} <br />{$goals} <br />{$budget} <br />{$timeframe} <br /><strong>{$find-me}</strong>";


// Send the mail using PHPs mail() function
mail($to, $subject, $message, $headers);


// Redirect
header("Location: thanks.html");


} else {

echo "There has been a problem in sending your email.";

}
?>

Along these lines, what books would people recommend for learning PHP? There are so many at the local bookstore that I don't know where to start.

Last edited by Will Phillips (2008-08-12 09:14:39)

Lorem ipsum dolor sit amet, consetetur sadipscing elitr.

Re: CC option for contact form.

$to = 'willphillipsdesign@gmail.com';
$to .= $email;

This will add a second address for the mail to goto! 

As far as PHP books go try Larry Ullman's PHP 6 and MySQL 5 which is a pretty good visual quickstart guide, or pick up the PHP Anthology by SitePoint as it has over 101 real world examples of PHP applications and scripts.

Last edited by angelsgeek (2008-08-12 10:51:53)

Re: CC option for contact form.

Will Phillips wrote:

GRAR! HULK HATE PHP! HULK SMASH SILLY PROGRAMMING CODE!

Oh yes, need to Twitter that one.

"I was blind, but now I see!"  John 9:25

Re: CC option for contact form.

Still could use some help sorting out this php stuff, everyone. Thanks!

Lorem ipsum dolor sit amet, consetetur sadipscing elitr.

Re: CC option for contact form.

Must...go...to...bed...

But before I do, I'd recommend getting some video tutorials instead/in addition to a book.  I got some from lynda.com, so check em out!  I found these much more helpful because you get to see someone doing it and you can follow along with the sample code and work on the same files the teacher is at the same time.

BTW, Montgomery lived up to his promise

Re: CC option for contact form.

Will,

Your headers code contains

$headers = "From: $email";
$headers = "Cc: $email\n"; 
$headers = "MIME-Version: 1.0\n";
$headers = "Content-type: text/html; charset=iso-8859-1\n";

This code is resetting the headers variable with new line. So in the end, $headers will only be equal to the last line. So you can modify it to be:

$headers = "From: $email\n";
$headers.= "Cc: $email\n";
$headers.= "MIME-Version: 1.0\n";
$headers.= "Content-type: text/html; charset=iso-8859-1\n";

This will include the CC line in the headers.

Re: CC option for contact form.

Ah, to the period at the end of the variable basically tells the script to, well, for lack of any technical term, skip over it?

Lorem ipsum dolor sit amet, consetetur sadipscing elitr.

Re: CC option for contact form.

$header is a string and you've initialized it with doing:

$headers = "From: me@domain.com";

So now $headers equals "From: me@domain.com"

Each time you do something like $headers = you are reassigning $headers to something else. So, if you did:

$headers = "hello world";

$headers no longer equals "From: me@domain.com" but now equals "hello world". So, you now just lost the initial information you assigned $headers with.

In programming you can concatenate strings, which simply means you can add to them - joining two string together, end to end. In PHP, you add to string by doing .= instead of just =

Here's an example:

$headers = "From: me@domain.com\n";
$headers.= "Content-type: text/html; charset=iso-8859-1\n";

Now, $headers will equal "From: me@domain.com\nContent-type: text/html; charset=iso-8859-1\n"

Does that make sense?

Re: CC option for contact form.

Think of the = sign as setting the variable to be exactly that, and any later occurrence will erase the variable and set it to the new string or number or whatever.

think of the .= sign as a + sign, you add on to it.  Alternatively you could write all your headers on one line, and that would just be a really long line.  Doing multiple lines with .= just makes it easier to read and understand.  But it's not actually a plus sign, meaning you can't to math with it!

for example

$num = 1;
$num = 2;
echo $num; //this returns:2

$num = 1;
$num .=2;
echo $num; //this returns 12

$num = 1;
$num +=2;
echo $num; //this returns 3

Last edited by C.Barr (2008-08-14 16:14:43)