PDA

View Full Version : help with email form code and php


graphics@sn
02-12-2010, 12:43 AM
I'm a graphic designer, slowly learning web design...
I'm just trying to build a simple form to enter classified ads and send the data to an email address using php.

Something is wrong with my form:


<form id="formclassified" name="formclassified" method="post" action="http://www.sonorannews.com/contactformprocess.php"><form action="contactformprocess.php" method="post" name="formclassified" class="center" id="formclassified" onsubmit="MM_validateForm('textfield','','R','textfield2','' ,'RisNum','textfield3','','RisEmail','textfield4', '','RisNum','textarea','','R');MM_validateForm('na me','','R','phone','','NisNum','email','','RisEmai l','category','','RisNum','adcopy','','R');return document.MM_returnValue">
<table width="666" height="258" border="0" cellpadding="0" cellspacing="0">
<tr>
<th width="111" height="36" scope="row"><p align="right" class="style5 style3">Name</p></th>
<td width="10">:</td>
<td width="545"><label for="name"></label>
<input name="name" type="text" id="name" size="65" maxlength="65" /></td>
</tr>
<tr>
<th height="41" scope="row"><div align="right" class="style4"><span class="style5 style3">Phone:</span></div></th>
<td>&nbsp;</td>
<td><label for="phone"></label>
<input name="phone" type="text" id="phone" size="12" maxlength="12" /></td>
</tr>
<tr>
<th height="39" class="style2" scope="row"><div align="right" class="style5 style4 style3">E-Mail:</div></th>
<td>&nbsp;</td>
<td><label for="email"></label>
<input name="email" type="text" id="email" size="65" maxlength="65" /></td>
</tr>
<tr>
<th height="43" scope="row"><div align="right" class="style5 style3">Category Number:</div></th>
<td>&nbsp;</td>
<td><label for="category"></label>
<input name="category" type="text" id="category" size="20" maxlength="20" /></td>
</tr>
<tr>
<th height="54" scope="row"><div align="right" class="style5 style3">Ad Copy:</div></th>
<td>&nbsp;</td>
<td><label for="adcopy"></label>
<textarea name="adcopy" id="adcopy" cols="65" rows="2"></textarea></td>
</tr>
<tr>
<th height="45" scope="row"><label for="button"></label></th>
<td>&nbsp;</td>
<td><input name="button" type="submit" id="button" onclick="MM_validateForm('name','','R','phone','','RisNum', 'email','','R','category','','R','adcopy','','R'); return document.MM_returnValue" value="Submit" /></td>
</tr>
</table>
<br />
<label for="button"></label>
</form>

PHP:

<?php

/* Email Variables */

$emailSubject = 'classified ad';
$webMaster = 'graphics@sonorannews.com';


/* Data Variables */

$nameField = $_POST['name'];
$phoneField = $_POST['phone'];
$emailField = $_POST['email'];
$categoryField = $_POST['category'];
$adcopyField = $_POST['adcopy'];



$body = <<<EOD
<br><hr><br>
Name: $name <br>
Phone: $phone <br>
Email: $email <br>
Category: $category <br>
Ad Copy: $adcopy <br>
EOD;

$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body,
$headers);


/* Results rendered as HTML */

$theResults = <<<EOD
<html>
<head>
<title>sent message</title>
<meta http-equiv="refresh" content"3;URL=http://www.sonorannews.com/classifiedads.html">
<style type="text/css">
<!--

<body>
Your email will be answered as soon as possible, thank you for reading Sonoran News.

</body>
</html>
EOD;
echo "$theResults";
?>

edbr
02-12-2010, 12:55 AM
you can help to de bug your self , I would
first try without the javascript validation ,
assuming its not that
add below to the begining of your mail script to check the post values are being passed

echo $_POST['name'];
echo $_POST['phone'];
echo $_POST['email'];
echo $_POST['category'];
echo $_POST['adcopy'];

graphics@sn
02-12-2010, 05:02 PM
I will try

tux
02-12-2010, 06:34 PM
Looks like you have used Dreamweavers wizards and maybe then tried editing it.

I will write something for you if can wait a bit.

Regards Paul

graphics@sn
02-12-2010, 06:47 PM
yes I used dreamweaver to make the form. Thank you so much for any help!!

tux
02-12-2010, 08:19 PM
Okay here we go. Firstly, if you can try to not use DW wizards that would be good.

Here is your form page, "contactform.php".....

<?php

$success = $_GET['success'];
$error = $_GET['error'];

if($success){
$success = "Your form was submitted, thank you.";
}elseif(!$success) $success = "";

if($error){
$error = "All fields are required! Please try again.";
}elseif(!$error) $error = "";


?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php echo $success; ?><?php echo $error; ?>
<form id="formclassified" name="formclassified" method="post" action="contactformprocess.php">
<input name="submitted" type="hidden" id="submitted" value="1" />

<table width="666" height="258" border="0" cellpadding="0" cellspacing="0">
<tr>
<th width="111" height="36" scope="row"><p align="right" class="style5 style3">Name</p></th>
<td width="10">:</td>
<td width="545"><label for="name"></label>
<input name="name" type="text" id="name" size="65" maxlength="65" /></td>
</tr>
<tr>
<th height="41" scope="row"><div align="right" class="style4"><span class="style5 style3">Phone:</span></div></th>
<td>&nbsp;</td>
<td><label for="phone"></label>
<input name="phone" type="text" id="phone" size="12" maxlength="12" /></td>
</tr>
<tr>
<th height="39" class="style2" scope="row"><div align="right" class="style5 style4 style3">E-Mail:</div></th>
<td>&nbsp;</td>
<td><label for="email"></label>
<input name="email" type="text" id="email" size="65" maxlength="65" /></td>
</tr>
<tr>
<th height="43" scope="row"><div align="right" class="style5 style3">Category Number:</div></th>
<td>&nbsp;</td>
<td><label for="category"></label>
<input name="category" type="text" id="category" size="20" maxlength="20" /></td>
</tr>
<tr>
<th height="54" scope="row"><div align="right" class="style5 style3">Ad Copy:</div></th>
<td>&nbsp;</td>
<td><label for="adcopy"></label>
<textarea name="adcopy" id="adcopy" cols="65" rows="2"></textarea></td>
</tr>
<tr>
<th height="45" scope="row"><label for="button"></label></th>
<td>&nbsp;</td>
<td><input name="button" type="submit" id="button" /></td>
</tr>
</table>
<br />
<label for="button"></label>
</form>

</body>
</html>

You will notice some PHP at the top which will handle success or errors and also some PHP in the page to echo the success or error.

Here is your process page, "contactformprocess.php"......

<?php
///////////////////////////////////////////////declare your variables here from the form on the previous page
$submitted = $_POST['submitted'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$category = $_POST['category'];
$adcopy = $_POST['adcopy'];

///////////////////////////////////////////////ensure required fields are filled in
if($submitted && $name && $phone && $email && $category && $adcopy){
///////////////////////////////////////////////
$to = 'graphics@sonorannews.com';//define the receiver of the email
$subject = "classified ad";//define the subject of the email
///////////////////////////////////////////////

///////////////////////////////////////////////dont change anything here
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: $email\r\nReply-To: $email";//put the 'From' & 'Reply To' email address here
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>


--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<!--Place your code here for plain text email-->
Details of Classified Ad.\n\n
Phone: <?php echo $phone; ?>\n
Email Address: <?php echo $email; ?>\n
Category Number: <?php echo $category; ?>\n
Advert: <?php echo $adcopy; ?>\n\n
<!--------------------------------------->

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<!--Place your code here for HTML email-->
<html>
<body>
<div id="email_body"><div class="innertube"><b>Message from the Contact Form.</b>
<p align="left" >Name: <b><?php echo $name; ?></b><br>
Phone: <b><?php echo $phone; ?></b><br>
Email Address: <b><?php echo $email; ?></b><br>
Category Number: <b><?php echo $category; ?></b><br>
Advert: <b><?php echo $adcopy; ?></b></p>
</body>
</html>
<!--------------------------------------->

--PHP-alt-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully redirect to success page
header("location:contactform.php?success=1");
exit;

}elseif($submitted){//if required fields not filled in redirect to error page
header("location:contactform.php?error=1");
exit;
}


?>

This will redirect back to the form page with a success or error message. Have a look through and see if you understand what is happening. Let me know if you dont understand anything and I'll try to explain.

Regards Paul

graphics@sn
02-12-2010, 09:46 PM
Thank you so much for your time it is so appreciated! I have a few more ads to design, then I will get busy on this!!!

nanny
02-16-2010, 10:13 PM
You will need to check that the email is valid e.g.
if (!preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email)) {
$error['email'] = 'Please enter a valid email address';
}
sometimes you might need to make sure the other variables are
also try jquery to keep on the same page- here is an example that works really well.
http://www.raymondselda.com/php-contact-form-with-jquery-validation/

Just change it to suit yourself..

graphics@sn
02-17-2010, 09:10 PM
I'm really new at this, actually, I don't even know how to enable php. Do I need to install something? mysql, apache? Sorry to be so untechnical, my background is more design than code.

tux
02-17-2010, 09:22 PM
Thats fine, we have all had to start somewhere.

When I got started a couple of years ago I used DavidJ's video tutorials here on this site, together with help from the guys on here including Dj himself.

If I was you I would run through the vids yourself and see how you get on. Its worth it.

http://www.dreamweaverclub.com/vtm/php-mysql-apache.php

Dj also offers 1to1 PHP training too, here....

http://codezenith.co.uk/

Good Luck

graphics@sn
02-17-2010, 09:51 PM
Good idea!