Be a Supporter!

Php: Contact Form

  • 1,724 Views
  • 10 Replies
New Topic Respond to this Topic
DannyIsOnFire
DannyIsOnFire
  • Member since: Apr. 14, 2005
  • Offline.
Forum Stats
Member
Level 21
Movie Buff
Php: Contact Form 2006-08-11 10:13:38 Reply

PHP: Main - All Your PHP Needs!

I know there has been a tutorial similar to this, but a little competition never hurt anyone, and i feel this tutorial is different enough to deserve its own thread.

Anyway, after reading this tutorial, you will be able to create a form that will send an email to a certain email address, with the contents of the form in it. This tutorial will also teach you how to make sure all the fields of the form have been written in, and if not it will take you to an error page. If they have, it will send the form. Simple.

To fully understand this tutorial you will need basic knowledge of HTML and PHP.

To start with, create the four files below, and upload them all to the same directory.
contact.php
contactform.php
contacterror.php
contactsuccess.php

In this tutorial i will be using the fields "name" "email" and "message", but you can change them if you want.

The next step, is to open up contact.php and type the following code.

<form action="contactform.php" method="post">
// opens the form and tells the browser to go to contactform,php when the send button is pressed.
Your Name: <input name="name">
// will display as Your Name: followed by an input box we have called name
<br>
// new line
Your Email: <input name="email">
// will display as Your Email: followed by an input box we have called email
<br>
// new line
Your Message: <textarea name="message"></textarea>
// will display as Your Message: followed by an multiline input box we have called message
<br>
// new line
<input type="submit" value="Send">
// displays a button with the word Send on it
</form>
// closes the form

Obviously you can edit the layout of the form, and add things like "size", but the script above is the bare bones of what we need.

Next, open up contactform.php

This is the script that is going to add protection, to make sure that the form isnt submitted without content.
E.G, if someone presses the send button without filling in all of the required fields, it will take them to contacterror.php. However, if someone fills in all of the fields, and hits submit, they will be taken to contactsuccess.php and there email will be sent.

Anyway, in contactform.php, type the following.

<?php
// opens the php script.
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];

if (empty($name) || empty($email) || empty($message)) {
header( "Location: contacterror.php" );
// if any of the fields are left empty, the user will be taken to contacterror.php

}
else {
mail( "you@youremail.com", "Contact Form",
"Name: $name\n\nMessage: $message\n\n",
"From: $email" );
header( "Location: contactsuccess.php

" );
}
// if all the required fields are filled in the email will be sent and the user redirected to contactsuccess.php
?>
// closes the php script

You need to change you@youremail.com to your email address obviously.

Now the final step is to go to contacterror.php and type something like
"It appears you have missed out a require field when filling in the contact form. Click here to go back and try again".

Then go to contactsuccess.php and type something like
"Success. Your email has been sent and you can expect a reply shortly.".

Now test your script, and it should all be working.
If it isnt, go back and check you havent made any mistakes.

Brought to you by DannyIsOnFire.
www.dannyisonfire.com


|| Portfolio || Facebook || Twitter ||

BBS Signature
Kings-Cant-Fall
Kings-Cant-Fall
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to Php: Contact Form 2006-08-11 10:35:48 Reply

I'd like to add that if you are hosting your own site you need to open smtp port and have a mail account set up...

Zwe
Zwe
  • Member since: May. 4, 2005
  • Offline.
Forum Stats
Member
Level 33
Blank Slate
Response to Php: Contact Form 2006-08-11 11:11:02 Reply

This can be used abusively easily. Someone could spam your inbox with as many emails as they want from nobody. It's good that you checked that they have entered something, but you should also check for valid email addresses etc


ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz

BBS Signature
DannyIsOnFire
DannyIsOnFire
  • Member since: Apr. 14, 2005
  • Offline.
Forum Stats
Member
Level 21
Movie Buff
Response to Php: Contact Form 2006-08-11 11:47:44 Reply

At 8/11/06 11:11 AM, Zwe wrote: This can be used abusively easily. Someone could spam your inbox with as many emails as they want from nobody. It's good that you checked that they have entered something, but you should also check for valid email addresses etc

Good idea.
If i ever make another tutorial on the subject, ill be sure to include that.

However, it would be pretty annoying to go back and keep sending the email over and over again.


|| Portfolio || Facebook || Twitter ||

BBS Signature
Zwe
Zwe
  • Member since: May. 4, 2005
  • Offline.
Forum Stats
Member
Level 33
Blank Slate
Response to Php: Contact Form 2006-08-11 11:58:02 Reply

At 8/11/06 11:47 AM, DannyIsOnFire wrote:
At 8/11/06 11:11 AM, Zwe wrote: This can be used abusively easily. Someone could spam your inbox with as many emails as they want from nobody. It's good that you checked that they have entered something, but you should also check for valid email addresses etc
Good idea.
If i ever make another tutorial on the subject, ill be sure to include that.

However, it would be pretty annoying to go back and keep sending the email over and over again.

Someone could use a bot for that. Going to the page where mail() takes place and setting the post variables automatically. Maybe a session should start on the contact.php page and set a session variable and check if it is set on the mail page, so then maybe the bot won't work.


ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz

BBS Signature
hooplah
hooplah
  • Member since: Feb. 18, 2007
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to Php: Contact Form 2007-04-02 19:53:38 Reply

What are the requirements for this tutorial?? because i have everything set up, however it doesnt work , i put it onto my apache server , tested out the form by typing in random stuff, and then ... it just didnt work , tried again ... but still no success. Does this form require php and myql? because if it does , ill have to upload to the host i use ... please, i need an answer, because i really want to have this working ASAP.


WHY HELLO THERE.

DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to Php: Contact Form 2007-04-02 19:58:04 Reply

At 4/2/07 07:53 PM, hooplah wrote: What are the requirements for this tutorial?? because i have everything set up, however it doesnt work , i put it onto my apache server , tested out the form by typing in random stuff, and then ... it just didnt work , tried again ... but still no success. Does this form require php and myql? because if it does , ill have to upload to the host i use ... please, i need an answer, because i really want to have this working ASAP.

It doesn't require MySQL.

Are you testing this locally?

If so, that's probably your problem because you need a mail server for it to work.

If you're testing it locally, I reccomend just uploading it to your host where the mail() function will be correctly configured.


BBS Signature
hooplah
hooplah
  • Member since: Feb. 18, 2007
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to Php: Contact Form 2007-04-02 21:04:16 Reply

It doesn't require MySQL.

Are you testing this locally?

If so, that's probably your problem because you need a mail server for it to work.

If you're testing it locally, I reccomend just uploading it to your host where the mail() function will be correctly configured.

How can I obtain this "mail server" ? ... I'm trying to make it send messages to my hotmail account ... is that why it doesnt work?


WHY HELLO THERE.

DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to Php: Contact Form 2007-04-02 23:20:04 Reply

At 4/2/07 09:04 PM, hooplah wrote: How can I obtain this "mail server" ? ... I'm trying to make it send messages to my hotmail account ... is that why it doesnt work?

Wait, what I mean is where is this all hosted? The from address doesn't matter.


BBS Signature
henke37
henke37
  • Member since: Sep. 10, 2004
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to Php: Contact Form 2007-04-03 02:00:59 Reply

This script got some worse issues, on many php versions this script allows anybody to attach any smtp header. Like the BCC header.
Then you just made it easier for the spammers to send junk mail. And you get blacklisted and can never send emails again, the end.


Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.

DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to Php: Contact Form 2007-04-03 02:04:26 Reply

At 4/3/07 02:00 AM, henke37 wrote: This script got some worse issues, on many php versions this script allows anybody to attach any smtp header. Like the BCC header.
Then you just made it easier for the spammers to send junk mail. And you get blacklisted and can never send emails again, the end.

That's why I always tell people to use a class like phpmailer to send emails. It takes care of all that nicely.

I keep meaning to write a tutorial for PHP main on it.


BBS Signature