Contact Form

Ask about a PHP problem here.
Post Reply
User avatar
pasqo83
Posts: 106
Joined: Sat May 14, 2011 6:23 am

Contact Form

Post by pasqo83 »

Hi,

I copied a contact form from a php book which I have used for a couple of years and I find it great as it does what I want but now I am about stunned on 2 things.

1) - I want to create a form where my user doesn't need to put anything inside that particular field (so it is mandatory). I still want validation that if they put something in there then it can only be letters and not numbers.

2) I have never ever been able to understand how to validate numbers only into a field as well.

Any help please? :)

Here is the php coding I have so loved and trusted over the years.
<?php 


/* Data Variables */


//Title
if (!empty($_REQUEST['title'])) {
	$title = ($_REQUEST['title']);
} else {
	$title = NULL;
	echo "Please enter your <u>Title </u>.<br>";
}


//First Name
if (!empty($_REQUEST['firstname'])) {
	$firstname = stripslashes(htmlspecialchars($_REQUEST['firstname']));
} else {
	$firstname = NULL;
	echo "Please enter your <u>first name</u>.<br>";
}


//Last Name
if (!empty($_REQUEST['lastname'])) {
	$lastname = stripslashes(htmlspecialchars($_REQUEST['lastname']));
} else {
	$lastname = NULL;
	echo "Please enter your <u>last name</u>.<br>";
}



//Position
if (!empty($_REQUEST['position'])) {
	$position = stripslashes(htmlspecialchars($_REQUEST['position']));
} else {
	$position = NULL;
	echo "Please enter your <u>position</u>.<br>";
}


//Company
if (!empty($_REQUEST['company'])) {
	$company = stripslashes($_REQUEST['company']);
} else {
	$company = NULL;
	echo "Please enter your <u>company</u>.<br>";
}


//tel
if (!empty($_REQUEST['tel'])) {
	$tel = stripslashes($_REQUEST['tel']);
} else {
	$tel = NULL;
	echo "Please enter your <u>telephone number</u>.<br>";
}


// Address 1
if (!empty($_REQUEST['address1'])) {
	$address1 = stripslashes($_REQUEST['address1']);
} else {
	$address1 = NULL;
	echo "Please enter the first line of your address<br>";
}



// Address 2
	if (!empty($_REQUEST['address2'])) {	
		$address2 = stripslashes(htmlspecialchars($_REQUEST['address2']));	
		} else {
	$address2 = NULL;
	echo "Please enter the second line of your address or type in n/a<br>";
}

// Address 3
	if (!empty($_REQUEST['address3'])) {	
		$address3 = stripslashes(htmlspecialchars($_REQUEST['address3']));	
	} else {
	$address3 = NULL;
	echo "Please enter the third line of your address or type in n/a <br>";
}


// Town/City
	if (!empty($_REQUEST['town'])) {	
		$town = stripslashes(htmlspecialchars($_REQUEST['town']));	
} else {
	$town = NULL;
	echo "Please enter your <u> Town/City </u>.<br>";
}


// Post Code
	if (!empty($_REQUEST['postcode'])) {	
		$postcode = stripslashes(htmlspecialchars($_REQUEST['postcode']));	
} else {
	$postcode = NULL;
	echo "Please enter your <u> post code </u>.<br>";
}



//Email Address
$email =($_REQUEST['email']);
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email));
 else {
	$email = NULL;
	echo "Please enter your <u>Email Address</u>.<br>";
}


// How
	if (!empty($_REQUEST['how'])) {	
		$how = stripslashes(htmlspecialchars($_REQUEST['how']));	
} else {
	$how = NULL;
	echo "Please enter <u>How you found us </u>.<br>";
}

// Other
	if (!empty($_REQUEST['other'])) {	
		$other = stripslashes(htmlspecialchars($_REQUEST['other']));
} else {
		$other = NULL;
	echo "Please enter where your found us or type n/a </u>.<br>";
}	




if ( $title && $firstname && $lastname && $position && $company && $address1 && $address2 && $address3 && $postcode  && $tel && $email && $town && $other && $how ) {


$to = "info@test.com";
$subject = "$firstname filled in form from A TESTING SITE";
$body = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<title></title>
</head>

<body bgcolor='#f9daa9' text='#000000'>
<center><table width='530' bgcolor='#ffffcc'>
	<tr>
		<td width='530'>
           <table border='0'  width='500' cellpadding='0'>
			<tr align='center'>	
            <img src='http://www.*******.com/templatefiles/03cf2ceb-bdbc-4723-ae49-c5c8e22ad565.jpg' width='525' height='150' border='0' style='background-position:center;' />
            </td>
			</tr>
			<tr></tr>
		<tr> 
            <td>
              
            <table width='92%' align='center'>
              <tr>
                <td colspan='4'> </td>
              </tr>
              <tr>
                <td colspan='4'><hr align='center' size='1' noshade='noshade' color='#000000'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr>
                <td colspan='4'></td>
              </tr>
			  
			  <tr> 
                <td colspan='4'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr>
                <td colspan='4'></td>
              </tr>
              <tr>
                <td width='26%'><div  align='right'><u> Title:</u></div></td>
                <td width='1%'> </td>
                <td width='58%'> $title</td>
                <td width='15%'> </td>
              </tr>
			  <tr>
                <td width='26%'><div  align='right'><u>First Name:</u></div></td>
                <td width='1%'> </td>
                <td width='58%'> $firstname </td>
                <td width='15%'> </td>
              </tr>
			  <tr>
                <td colspan='4'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr>
                <td colspan='4'></td>
              </tr>
              <tr>
                <td width='26%'><div  align='right'><u>Last Name:</u></div></td>
                <td width='1%'> </td>
                <td width='58%'> $lastname </td>
                <td width='15%'> </td>
              </tr>
			  <tr> 
                <td colspan='4'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr>
                <td colspan='4'></td>
              </tr>
              <tr>
                <td width='26%'><div  align='right'><u>Telephone Number:</u></div></td>
                <td width='1%'> </td>
                <td width='58%'> $tel </td>
                <td width='15%'> </td>
              </tr>
             <tr> 
                <td colspan='4'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr>
                <td colspan='4'></td>
              </tr>
              <tr>
			    <td width='26%'><div  align='right'><u> Email Address:</u></div></td>
                <td width='1%'> </td>
                <td width='58%'> $email</td>
                <td width='15%'> </td>
              </tr>
			  
              <tr>
                <td colspan='4'><hr align='center' size='1' noshade='noshade' color='#000000'></td>
              </tr>
			  <tr> 
                <td colspan='4'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr>
                <td colspan='4'></td>
              </tr>
              <tr>
                <td width='26%'><div  align='right'><u> Company:</u></div></td>
                <td width='1%'> </td>
                <td width='58%'> $company</td>
                <td width='15%'> </td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr>
                <td colspan='4'></td>
              </tr>
              <tr>
                <td width='26%'><div  align='right'><u> Position:</u></div></td>
                <td width='1%'> </td>
                <td width='58%'> $position</td>
                <td width='15%'> </td>
              </tr>
			  <tr>
                <td colspan='4'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr>
                <td colspan='4'></td>
              </tr>
			  
              <tr>
                <td width='26%'><div  align='right'><u> Address 1:</u></div></td>
                <td width='1%'> </td>
                <td width='58%'> $address1 </td>
                <td width='15%'> </td>
              </tr>
              <tr>
                <td colspan='4'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr>
                <td colspan='4'></td>
              </tr>
              <tr>
                <td width='26%'><div  align='right'><u> Address 2:</u></div></td>
                <td width='1%'> </td>
                <td width='58%'> $address2 </td>
                <td width='15%'> </td>
              </tr>
              <tr>
                <td colspan='4'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr>
                <td colspan='4'></td>
              </tr>
              <tr>
                <td width='26%'><div  align='right'><u> Address 3:</u></div></td>
                <td width='1%'> </td>
                <td width='58%'> $address3 </td>
                <td width='15%'> </td>
              </tr>
			  <tr>
                <td colspan='4'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr>
                <td colspan='4'></td>
              </tr>
              <tr>
                <td width='26%'><div  align='right'><u> Town:</u></div></td>
                <td width='1%'> </td>
                <td width='58%'> $town </td>
                <td width='15%'> </td>
              </tr>
			  <tr> 
                <td colspan='4'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr>
                <td colspan='4'></td>
              </tr>
              <tr>
                <td width='26%'><div  align='right'><u> postcode:</u></div></td>
                <td width='1%'> </td>
                <td width='58%'> $postcode </td>
                <td width='15%'> </td>
              </tr>
             <tr> 
                <td colspan='4'><hr align='center' size='1' noshade='noshade' color='#000000'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr>
                <td colspan='4'></td>
              </tr>
              <tr>
                <td width='26%'><div  align='right'><u> How Did you find us:</u></div></td>
                <td width='1%'> </td>
                <td width='58%'> $how</td>
                <td width='15%'> </td>
              </tr>
			  <tr> 
                <td colspan='4'></td>
              </tr>
              <tr> 
                <td colspan='4'></td>
              </tr>
              <tr>
                <td colspan='4'></td>
              </tr>
              <tr>
                <td width='26%'><div  align='right'><u>Other:</u></div></td>
                <td width='1%'> </td>
                <td width='58%'> $other</td>
                <td width='15%'> </td>
              </tr>
			  <tr>
                <td colspan='4'><hr align='center' size='1' noshade='noshade' color='000000'></td>
              </tr>
               <tr>
                <td colspan='4'> </td>
              </tr>
              <tr>
                <td colspan='4'> </td>
              <tr>
                <td> </td>
                <td> </td>
                <td colspan='2'> </td>
              </tr>
              <tr>
                <td colspan='4'></td>
              </tr>
            </table>
            </td>
			</tr>
			<tr>	
            <td bgcolor='#ffffcc' style='color:#000000;' height='20' align='center'><small> Copyright 2011 © tester. All Rights Reserved.</small></center></td>
			</tr>
		  </table>       		
		</td>
	</tr>
</table>
</body></center>
</html>";


$headers = 'From: finance@testing.com' . "\r\n" .
	'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

	
echo "<p>Thank you for your enquiry $firstname!<br>We will be in touch soon.</p>

<p> </p>

<p> You will be re-directed to our home page in 5 seconds</p>\n";
	
	echo "<meta http-equiv=\"Refresh\" content=\"5;url = http://www.test@testing.com/\">";
	
} else {
 
 echo "<br>Please go back and fill in the missing field(s).";


 
 }

?>
  • Empty your mind, be formless like water. If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot it becomes the teapot. Now, water can flow or it can crash. Be water my friend.
User avatar
ta2shop
Posts: 179
Joined: Sat May 07, 2011 9:07 am
Location: madrid, Spain
Contact:

Re: Contact Form

Post by ta2shop »

hy, first thing, you could scrap, this code and think about making your own, it is wery easy.
secound, you sould really start making your own code!
at least give it a try, post what you are doing here and i am sure wii will by able to help you out.
Image
User avatar
pasqo83
Posts: 106
Joined: Sat May 14, 2011 6:23 am

Re: Contact Form

Post by pasqo83 »

ta2shop wrote:hy, first thing, you could scrap, this code and think about making your own, it is wery easy.
secound, you sould really start making your own code!
Thanks ta2shop, my plans are exactly just that, i am just waiting to get that understanding so I can make my own contact form coding. Its just I've seen so many different versions of it my head is fizzled. :oops:

I will be writing my own one shortly, I will do a basic one then improve onto it, watch this space :D

With that contact form php I sent, the idea was how simple the coding was. But what is missing from the coding is the telephone number php coding, I have looked for php coding for it but never worked within this BUT this morning after I sent this and I got my mailing list to work, I am starting to get a better understanding of it, so I think I am better off just doing some trial and error and see what I get first :D

There was one thing though, within this coding or any form coding, if I wanted validation lets say I just want characters inside it (no html) - but the user doesn't have to fill in that field (example, Address 1, Line 2, Line 3, City, Post Code) - I want line 1 and line 2 to have validation if something is put in there but the customer does not have to put something in there if there is no need to.
  • Empty your mind, be formless like water. If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot it becomes the teapot. Now, water can flow or it can crash. Be water my friend.
User avatar
ta2shop
Posts: 179
Joined: Sat May 07, 2011 9:07 am
Location: madrid, Spain
Contact:

Re: Contact Form

Post by ta2shop »

yes you could do just that.
you can strat by making the html form for it, post it here and i will give you the start on the php coding!
Image
User avatar
pasqo83
Posts: 106
Joined: Sat May 14, 2011 6:23 am

Re: Contact Form

Post by pasqo83 »

ta2shop wrote:yes you could do just that.
you can strat by making the html form for it, post it here and i will give you the start on the php coding!
Thanks ta2shop, i am at work, i work in an office so I am going to spend all day in creating a php form and testing it tonight, so excited. here is the html code.
<html>
<head>
<title>Contact Form</title>
</head>

<body>

<form action="#" method="post">

<label for="name">Name</label><br />
<input type="text" name="name" value="" />

<p></p>

<label for="tel">Tel</label><br />
<input type="text" name="tel" value="" />

<p></p>

<label for="email">Email</label><br />
<input type="text" name="name" value="" />

<p></p>


<label for="message">Message</label><br />
<textarea name="message" rows="7" cols="50" value="" ></textarea>

<p></p>

<input type="submit" value="submit" />


</form>

</body>
  • Empty your mind, be formless like water. If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot it becomes the teapot. Now, water can flow or it can crash. Be water my friend.
User avatar
ta2shop
Posts: 179
Joined: Sat May 07, 2011 9:07 am
Location: madrid, Spain
Contact:

Re: Contact Form

Post by ta2shop »

you dont really need to put the label and value in for text input's !
you can just use itl ike so:
<form>
   Your Name: <input type="text" name="name" >
</form>
you can use the value tag for the submit button:
<input type="submit" name="send" value="Send">
keep up the good work! :D
Image
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: Contact Form

Post by Tino »

No, you'll want those labels. They make your forms a lot more accessible, which is very important in design!
Please check out my CodeCanyon items.
User avatar
pasqo83
Posts: 106
Joined: Sat May 14, 2011 6:23 am

Re: Contact Form

Post by pasqo83 »

Thanks ta2shop,

The issue I have is, im not advanced enough to write my own code. Im not familiar enough with the php expressions to do this, so I can read websites now and have and understand it a bit better.
I know my code needs to say

"if name, tel, email and message has something in there and the form is valid, then send a sucess message, if errors then list errors"

but to write that out myself..... its like alacazam at the moment :( just going to read up some of the statements and how they function.
  • Empty your mind, be formless like water. If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot it becomes the teapot. Now, water can flow or it can crash. Be water my friend.
User avatar
pasqo83
Posts: 106
Joined: Sat May 14, 2011 6:23 am

Re: Contact Form

Post by pasqo83 »

Tino wrote:No, you'll want those labels. They make your forms a lot more accessible, which is very important in design!
I always put value="" in all my coding, as I use the php expression inside it of value='<? php echo='name' ?>' so if the user puts an error in one of the form fields, they don't need to start again.
  • Empty your mind, be formless like water. If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot it becomes the teapot. Now, water can flow or it can crash. Be water my friend.
User avatar
ta2shop
Posts: 179
Joined: Sat May 07, 2011 9:07 am
Location: madrid, Spain
Contact:

Re: Contact Form

Post by ta2shop »

yeh :? forgot about that, yeh i guess it is good practice to have that!
but if your contact form will have 2 or 3 fields, dont see the point of doing that ;)
Image
User avatar
pasqo83
Posts: 106
Joined: Sat May 14, 2011 6:23 am

Re: Contact Form

Post by pasqo83 »

ta2shop wrote:if your contact form will have 2 or 3 fields, dont see the point of doing that ;)
My forms are alot bigger than that, I am using this form as an example because I want to try and learn to do a small 1 because the bigger one will be easier after.

You validate name which will be a text, any other field that is text will have the same validation as name. I will just need to do new $ for them. :D Email only happens once and tel number can be landline and then mobile number. :D
  • Empty your mind, be formless like water. If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot it becomes the teapot. Now, water can flow or it can crash. Be water my friend.
Post Reply