Contact Form Problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sully210
Forum Newbie
Posts: 17
Joined: Sat Sep 11, 2010 4:09 pm

Contact Form Problems

Post by sully210 »

I will start by apologizing for my ignorance as I am brand new at this.

I have built two sites for clients using templates from dreamtemplate.com and they include php contact forms. I have gotten the contact forms to work but cannot get all of the fields to show up in the email that is sent. I have modified fields to suit the needs of the clients and the only information I get when I submit a test form are the variables they installed???

Here is the page: http://westerlyfurniture.com/contact.html

Here is the HTML File:

<!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>
<title>contact</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script src="Scripts/swfobject_modified.js" type="text/javascript"></script>
<script type="text/javascript">
// <![CDATA[
jQuery(document).ready(function(){
$('#contactform').submit(function(){
var action = $(this).attr('action');
$.post(action, {
name: $('#name').val(),
address: $('#address').val(),
email: $('#email').val(),
phone: $('#phone').val(),
message: $('#message').val()
},
function(data){
$('#contactform #submit').attr('disabled','');
$('.response').remove();
$('#contactform').before('<p class="response">'+data+'</p>');
$('.response').slideDown();
if(data=='Message sent!') $('#contactform').slideUp();
}
);
return false;
});
});
// ]]>
</script>
</head>
<body>
<div class="main">
<div class="header">
<div class="flash">
<object id="FlashID" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="1000" height="192">
<param name="movie" value="header_contact.swf" />
<param name="quality" value="high" />
<param name="wmode" value="opaque" />
<param name="swfversion" value="8.0.35.0" />
<!-- This param tag prompts users with Flash Player 6.0 r65 and higher to download the latest version of Flash Player. Delete it if you don’t want users to see the prompt. -->
<param name="expressinstall" value="Scripts/expressInstall.swf" />
<!-- Next object tag is for non-IE browsers. So hide it from IE using IECC. -->
<!--[if !IE]>-->
<object type="application/x-shockwave-flash" data="header_contact.swf" width="1000" height="192">
<!--<![endif]-->
<param name="quality" value="high" />
<param name="wmode" value="opaque" />
<param name="swfversion" value="8.0.35.0" />
<param name="expressinstall" value="Scripts/expressInstall.swf" />
<!-- The browser displays the following alternative content for users with Flash Player 6.0 and older. -->
<div>
<h4>Content on this page requires a newer version of Adobe Flash Player.</h4>
<p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/down ... player.gif" alt="Get Adobe Flash player" width="112" height="33" /></a></p>
</div>
<!--[if !IE]>-->
</object>
<!--<![endif]-->
</object>
</div>
</div>

<div class="body">
<div class="body_resize">
<div class="left">
<h2>Contact Affordable Furnishings</h2>
<p class="back"> Please feel free to fill out our contact form for any additional information you may need or to set up an appointment for a personalized catalog and showroom tour. Please be as detailed as possible&nbsp;and remember that you can always reach us during business hours at (401) 348-6363</p>
<div class="clr"></div>
<h2>Contact form</h2>
<form action="contact.php" method="post" id="contactform">
<ol>
<li>
<label for="name">Name<a href="#">*</a><br />
</label>
<input name="name" class="text" id="name" />
</li>
<li>
<label for="address"><a href="#"></a>Address<br />
</label>
<input id="address" name="address" class="text" />
</li>
<li>
<label for="email">Email*<br />
</label>
<input id="email" name="email" class="text" />
</li>
<li>
<label for="phone">Phone<br />
</label>
<input id="phone" name="phone" class="text" />
</li>
<li>
<label for="message">Message<a href="#">*</a><br />
</label>
<textarea id="message" name="message" rows="6" cols="50"></textarea>
</li>
<li class="buttons">
<input type="image" name="imageField2" id="imageField2" src="images/send.gif" />
</li>
</ol>
</form>
</div>
<div class="right">
<h2>Contact info </h2>
<div class="bg"></div>
<p><strong>Alternate Contact Options</strong></p>
<p>You can reach us by phone or a simple email with the information provided below.</p>
<p>(401) 348-6363<br />
<strong>info@westerlyfurniture.com</strong><br />
</p>
<div class="bg"></div>
<p><strong>Hours</strong></p>
<p>We are open Monday through Friday from 10:00 a.m. until 5:00 p.m., and Saturday from 10:00 a.m. until 3:00 p.m.<br />
<strong>Please click on the map for detailed driving directions to our showroom.</strong><br />
</p>
<p><a href="http://maps.google.com/maps?hl=en&um=1& ... IwAA"><img src="images/map.png" width="256" height="224" /></a></p>
</div>
<div class="clr"></div>
</div>
<div class="clr"></div>
</div>
<div class="footer">
<div class="resize">

<div>© Copyright 2010 Affordable Furnishings. All Rights Reserved<br />
<a href="index.html">Home</a> | <a href="contact.html">Contact</a> | <a href="sitemap.html">Sitemap</a></div>
</div>
<p class="clr"></p>
</div>
</div>
<script type="text/javascript">
<!--
swfobject.registerObject("FlashID");
//-->
</script>
</body>
</html>

Here is the PHP script:

<?php

if(!$_POST) exit;

$email = $_POST['email'];


//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','address','email','phone','message');
$required = array('name','address','email','phone','message');

$your_email = "info@westerlyfurniture.com";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";

foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'company') {
if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
}

if(@mail($your_email,$email_subject,$email_content)) {
echo 'Message sent!';
} else {
echo 'ERROR!';
}
}
?>

The only information that is delivered to the email address specified is:

new message:
name: jeff Sullivan
email: sully@210usa.com
message: testing third time

I need the email sent to deliver all the information submitted in the contact form and would also like to know how to add extra fields if necessary. I would obviously want them to be included in the email that is finally sent.

Once again I will apologize for my lack of knowledge and expertise. I am trying :oops:
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: Contact Form Problems

Post by mecha_godzilla »

I think you need to look at this part of the PHP script:

Code: Select all

$values = array ('name','address','email','phone','message');
$required = array('name','address','email','phone','message');
These are the only values that the PHP script will look at (and all it does is check whether the 'email' value is the right format and that the other values aren't empty). The script doesn't quite make sense because (looking at the form on the site that you linked to) the address and phone number are not required, but according to your PHP script these values are in the $required array and are therefore supposed to return an error if they're empty.

To capture new values, you just need to add them to the arrays like this

Code: Select all

$values = array ('name','address','email','phone','message','favourite_song');
$required = array('name','address','email','phone','message','favourite_song');
and of course update your HTML form with matching text fields.

Just for future reference (assuming you're not familiar with PHP) the basic way to capture form values is

Code: Select all

$name = $_POST['name']
but this is essentially unvalidated and therefore dangerous. Something like this would be better:

Code: Select all

if (!get_magic_quotes_gpc()) {
    $name = addslashes(strip_tags(trim($_POST['name'])));
    $name = @substr($name, 0, 100);
} else {
    $name = strip_tags(trim($_POST['name']));
    $name= @substr($name, 0, 100);
}
What that code does is capture the value, trim it ("Strip whitespace (or other characters) from the beginning and end of a string"; from the PHP manual), remove anything that basically looks like code and then truncate it to 100 characters (to stop buffer overflows). You have to have a get_magic_quotes_gpc() check of some kind because this affects the way that single quotes (') are managed - these can break database queries if they're not properly validated, but that's for another day I guess :)

Get the code you've got working first, then ask if you need any help on validation. If you get really stuck, you can always echo() out the values to make sure the PHP script has captured them properly; modify the relevant part of the script were it looks like this:

Code: Select all

foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'company') {
if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
}
to this:

Code: Select all

foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'company') {
if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."\n";
echo $value . ' : ' . $_POST[$value] . '<br />';
}
}
HTH,

Mecha Godzilla
sully210
Forum Newbie
Posts: 17
Joined: Sat Sep 11, 2010 4:09 pm

Re: Contact Form Problems

Post by sully210 »

Holy Crap......Thank you. That is awesome and you are a genius :)
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: Contact Form Problems

Post by mecha_godzilla »

You're welcome, but I assure you that I'm no genius :lol:

M_G
Post Reply