PDA

View Full Version : [TUT] Making a contact form.



Drompo
22-02-2007, 12:15 PM
Hello, I am going to take you through the nessecery steps to making a contact form.
First off i will explain what i am going to do.

I am going to make two files:
- One to show the contact form (contact.php)
and one to send the contact form. (sendmail.php)

First make a blank page in Notepad or Dreamweaver.
Before the actull code add this.

<?php
$ipi = getenv("REMOTE_ADDR");
?>That will get the users IP address
The first line of code is the beginning of the form and also tells the data where to output.

<form method="post" action="sendmail.php">
You can change the "sendmail.php" to whatever you like as long as it complies with the output file.

Now you need to work out what fields you want. Inn this instance we are going to use:
-Name (Text Box)
-Email Address (Text Box)
-Contact Reason (Drop Down Box)
-Message (Text Area)

I will list the input codes for various inputs later on.

Now to show the Name Box we would put;

Name: <input type="text" name="name" size="30" />

Now to show the Email Address Box we would put;

Email Address: <input type="text" name="email" size="30" /><br />

Now the Drop down menu is more complicated.

Contact Reason:<br />
<select name="reason" size="1">
<option value="Website Problem">Website Problem</option>
<option value="Billing Problem">Billing Problem</option>
<option value="Technical Support">Technical Support </option>
<option value="Report A Staff Member ">Report A Staff Member</option>
<option value="Other">Other (Please Specify in message)</option>
</select>

The Name after "Option value="NAME" that is the tag that will be the subject of the email you recieve.

Finally The Main message.

<textarea name="main" rows="4" cols="30"></textarea>

You can make the area bigger/smaller by changing the numbers above.

and now the user will submit the form

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

and then end the form

</form>

In the End Looking like this

<form method="post" action="sendmail.php">
Email Address: <input type="text" name="email" size="30" /><br />
Contact Reason:<br />
<select name="reason" size="1">
<option value="Website Problem">Website Problem</option>
<option value="Billing Problem">Billing Problem</option>
<option value="Technical Support">Technical Support </option>
<option value="Report A Staff Member ">Report A Staff Member</option>
<option value="Other">Other (Please Specify in message)</option>
</select>
<textarea name="main" rows="4" cols="30"></textarea><br />
<input type="submit" value="Send" />
</form>


Now Call this sendmail.php


<?php
if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))
{
die ("Invalid Email Address");
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;
}
if(empty($visitor) || empty($visitormail) || empty($notes )) {
die ("You didn't fill in all the fields");
}

$subject = $reason;

$message = "Reason: $reason \n
Message: $main \n
From: $visitor ($visitormail)\n
Additional Info : IP = $ip \n
";

$from = "From: $email\r\n";


mail("YourEmail", $subject, $message, $from);

?>

Do Not edit that code unless you know what your doing.
Where it says
mail("YourEmail", $subject, $message, $from);
Edit Youremail to The email address you want it to send it to.

And there you have your contact form.


Types Of Inputs:

Text Field

Text Field:
<input type="text" name="fieldname">

Radio Buttons

Options:
<input type="radio" name="fieldname" value="option 1"> Option 1
<br>
<input type="radio" name="fieldname"" value="option 2"> Option 2

Checkboxes

Options:
Option 1
<input type="checkbox" name="fieldname" value="option 1" />
<br />
Option 2
<input type="checkbox" name="fieldname" value="option 2" />

Drop Down Box

Options:
<select name="options">
<option value="Option 1">Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
</select>

TextAreas

<textarea name="fieldname" rows="4" cols="30"></textarea>



Edited by L&#181;ke (Forum Moderator): Thread Moved From Website Designing. Please post in the correct section next time, Thanks :).

Isolde
22-02-2007, 12:21 PM
Nice, i like the tut. Very helpful. +rep

Cixso
22-02-2007, 12:44 PM
Just what I needed!!!

+rep. This should get stickied

Isolde
22-02-2007, 12:56 PM
You could've just asked me for a contact form Danny, i have same one lol,

-::Mr.Dave::-
22-02-2007, 02:19 PM
Excellent +REP

Drompo
22-02-2007, 02:23 PM
Is it possible that this could be moved to Web Tuts?

Isolde
22-02-2007, 03:02 PM
Request it by a mod.

nets
22-02-2007, 03:42 PM
die ("Invalid Email Address");
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;

That wont be outputted.

DJ_CJ
22-02-2007, 03:44 PM
well done, i needed a contact form :D
+rep

Drompo
22-02-2007, 03:50 PM
Oops i was goning to use badinput but it didn't work

The contact form will still work though

DJ_CJ
23-02-2007, 11:39 AM
in sendmail.php i had to change :
if(empty($visitor) || empty($visitormail) || empty($notes )) {
to:
if(empty($name) || empty($email) || empty($main )) {
but other then that its great :D

Drompo
23-02-2007, 11:47 AM
This is an updated code. Thanks to DJ CJ for pointing this out

Now Call this sendmail.php


<?php
if(!$email== "" && (!strstr($email,"@") || !strstr($email,".")))
{
die ("Invalid Email Address");
}
if(empty($name) || empty($email) || empty($main)) {
die ("You didn't fill in all the fields");
}

$subject = $reason;

$message = "Reason: $reason \n
Message: $main \n
From: $visitor ($visitormail)\n
Additional Info : IP = $ip \n
";

$from = "From: $email\r\n";


mail("YourEmail", $subject, $message, $from);

?>Do Not edit that code unless you know what your doing.
Where it says
mail("YourEmail", $subject, $message, $from);Edit Youremail to The email address you want it to send it to.

There you go.

DJ_CJ
23-02-2007, 06:20 PM
yh thats better. your welcome :D

Blob
23-02-2007, 07:04 PM
How are you declaring $email etc? Post? Get? File?

Drompo
23-02-2007, 07:27 PM
The Form is post

Blob
23-02-2007, 07:31 PM
And do you have

$email = $_POST["email"];

Drompo
23-02-2007, 07:32 PM
The input form sends the data to sendmail.php

Mentor
24-02-2007, 10:41 AM
How is it getting the post data? or are you neglecting to take in to account alot of peoples php set up doesnt have the automatic post/get vars to there names transformations on as there a securty risk? Hence meaning the script wont work on most setups o.0

Blob
24-02-2007, 11:06 AM
Mentor said what I was trying to say in one post, well done.

Heres update code for you:


<form method="post" action="sendmail.php">
Email Address: <input type="text" name="email" size="30" /><br />
Contact Reason:<br />
<select name="reason" size="1">
<option value="Website Problem">Website Problem</option>
<option value="Billing Problem">Billing Problem</option>
<option value="Technical Support">Technical Support </option>
<option value="Report A Staff Member ">Report A Staff Member</option>
<option value="Other">Other (Please Specify in message)</option>
</select>
<textarea name="main" rows="4" cols="30"></textarea><br />
<input type="submit" value="Send" />
</form>


<?php
$email = $_POST["email"];
$name = $_POST["name"];
$main = $_POST["name"];
$reason = $_POST["reason"];
$ip = $_SERVER["REMOTE_ADDR"];

if(!$email== "" && (!strstr($email,"@") || !strstr($email,".")))
{
die ("Invalid Email Address");
}
if(empty($name) || empty($email) || empty($main)) {
die ("You didn't fill in all the fields");
}

$subject = $reason;

$message = "Reason: $reason \n
Message: $main \n
From: $email \n
Additional Info : IP = $ip \n
";

$from = "From: $email\r\n";


mail("YourEmail", $subject, $message, $from);

?>

That should work?

Want to hide these adverts? Register an account for free!