Home N-13 News Forums Help Search
RegisterForgot password?
How to add image to post necklacesdiscou
Known bugs - 4.0.3 necklacesdiscou

Latest N-13 News 4.0.3

What is N-13 News?
Where can I get help?
Known bugs

Forums Tutorials Simple Formmail Script
Chronocalamity
Posted on 05 Jan 2008, 02:08:19

Access: Member
Total Posts: 6
Joined: 2008-01-05

This is something I wanted to know about 3 years ago when i started web design so to save your the "WTF DO I NEED TO DO" speech that im sure youve said already so here it is

1st thing you need is a 3 field form, standerd HTML stuff but ill run over it anyway

Use GET for your form
Code:
<form id="form1" name="form1" method="get" action="form2.php">
For the first field the "Name" field
Code:
<input type="text" name="name" />
Second Field is the "Email" Field
Code:
<input type="text" name="email" />
Final field is a multitext field for "comments"
Code:
<textarea name="msg" rows="5"></textarea>

Theres your html base for the form and make sure u keep the names the same if you dont wanna have to change the vars in the php

Second part of the formmail
Of course THE FUN PHP PART

First of you need to make some variables and ill explain what they do dont worry

Code:
<?php 
$address 
= $_SERVER['remote_addr'];
?>

This var gets the users IP address so if they send u hate mail u can ban um or whatever

Code:
<?php 
$name 
= addslashes($_GET['name']);
$email = addslashes($_GET['email']);
$msg = addslashes($_GET['msg']);
?>

These vars just grab the info entered into the form so the script can use the info

And now we have our starting vars for now

now since we have our info from the vars above we can get that info sent to a mail address like this

when ya get a email its broke up in parts right, theres the From,subject, and then the message itself sections well we sorta have to define all that junk like below

Code:
<?php 
$header 
= "From: $email"; 
$subject = "My contact Form";
$mymail = "youremail@yoursite.com"; //Put your email address here
$smsg = "Visitor's IP: $address\n"
. "name: $name\n"
. "email: $email\n"
. "msg: $msg\n";
?>


This is the message body right here
1st it will give ya the ip address of who used the form then whatever was in the name field and the same for email and msg

this is a workable formmail script without Validation
witch i might wright that part in later but this post would be realy long if i did it all in one pass

Im fairly new to php but i got alot of time on my hands and i saw that "Chris" was the only one putting
tuts up so i said why not
#133
Last edited by Chronocalamity at 2008-01-05 03:21:29 Reason:
Chronocalamity
Posted on 05 Jan 2008, 03:28:50

Access: Member
Total Posts: 6
Joined: 2008-01-05

Lol i almost forgot to post the mailing part of it witch is done with a simple line of code

Code:
<?php 
mail 
($mymail,$subject,$smsg,$header);
?>

This is like the most important part cant beleave i forgot it
but since im here might as well go over Validation

Code:
<?php 
if (strlen($name) == 0 )
{
die(
"<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid name</font></p>");
}

if (! 
ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email))
{
die(
"<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid email</font></p>");
}

if (
strlen($email) == 0 )
{
die(
"<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid email</font></p>");
}

if (
strlen($msg) <5)
{
die(
"<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid msg</font></p>");
}
if (
strlen($msg) >500)
{
die(
"<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid msg</font></p>");
}

if (
strlen($msg) == 0 )
{
die(
"<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid msg</font></p>");
}
?>


Witch all that above does is make sure someone dont just hit submit over and over and keep sending you a blank email plus it sends the error back to the user telling them whats wrong
#138
Chronocalamity
Posted on 05 Jan 2008, 03:33:18

Access: Member
Total Posts: 6
Joined: 2008-01-05

all in all the script looks like this
Code:
<?php 
<?
// script made by chronocalamity under the law "wtf u want with it but give me my props" 

// Vars
$address = $_SERVER['REMOTE_ADDR'];
$name = addslashes($_GET['name']);
$email = addslashes($_GET['email']);
$msg = addslashes($_GET['msg']);

// Validation
if (strlen($name) == 0 )
{
die(
"<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid name</font></p>");
}

if (! 
ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email))
{
die(
"<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid email</font></p>");
}

if (
strlen($email) == 0 )
{
die(
"<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid email</font></p>");
}

if (
strlen($msg) <5)
{
die(
"<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid msg</font></p>");
}
if (
strlen($msg) >500)
{
die(
"<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid msg</font></p>");
}

if (
strlen($msg) == 0 )
{
die(
"<p align='center'><font face='Arial' size='3' color='#FF0000'>Please enter a valid msg</font></p>");
}
// sending the mail
$header = "From: $email";
$subject = "Test contact Form";
$mymail = "youremail@yoursite.com";
$smsg = "Visitor's IP: $address\n"
. "name: $name\n"
. "email: $email\n"
. "msg: $msg\n";

mail ($mymail,$subject,$smsg,$header);
?>
?>
#139
Chronocalamity
Posted on 05 Jan 2008, 03:34:57

Access: Member
Total Posts: 6
Joined: 2008-01-05

And just for fun your HTML should look like this
Code:
<body>
<form id="form1" name="form1" method="get" action="form2.php">
<p>
<label>name
<input type="text" name="name" />
</label>
</p>
<p>
<label>email
<input type="text" name="email" />
</label>
</p>
<p>
<label>msg
<textarea name="msg" rows="5"></textarea>
</label>
</p>
<p>
<label>
<input type="submit" name="Submit" value="Submit" />
</label>
</p>
</form>
</body>
#140
Network-13.com © 2013