PDA

View Full Version : || Basic PHP Form ||



Dan Williamson
21-11-2005, 11:10 AM
Hi.

For my second PHP tutorial i'm going to tell you how to code a simple form with a simple PHP code.

This tutorial is written and copyright to me :)

Here we go:

Well first the easy bit.

Creating the forms layout. and here we go.




<form action="mail.php" method="post">
Name <input type="text" name="name"><br/>
Email addess <input type="text" name="email"><br/>
Message<br/>
<textarea name="message" cols="40" rows="5"></textarea><br/>
<input type="submit" value="send"><br/>
</form>





Now i'm sure your all familiar with a simple form layout so i kept it simple.

Now you must name the PHP file mail.php if you don't want to change anything on the form layout.

Heres the PHP code i'll explain in the PHP code so it doesn't get confusing.




<?php

//variables (You can change the Variables)

$youremail = "[email protected]";
// your email address

$subject = "Contact";
// the subject of the email

$formsent = "formsent.php";
// The re-direct page in which it can say Form sent

// Don't change anything else on this script

if($email == ""){
?>
No email address added. Please go back.<br/>
<?php
}elseif($name == ""){
?>
No name added. Please go back.<br/>
<?php
}elseif($message == ""){
?>
No message added. Please go back.<br/>
<?php
}else{

$msg = ereg_replace("\\\'", "'", $message);
$msg = ereg_replace('\\\"', "\"", $msg);
$message1 = "from: $name\nemail: $email\nmessage:\n$msg1";

mail($youremail, $subject, $msg, "From: $email\r\nReply-to: $email\r\n");
?>
<meta http-equiv="refresh" content="0; url=<?echo $formsent;?>"">
<?php
}
?>



Now the bits with // are explaining what to do, after reading many PHP tutorials for CMS's i've seen people do this.

I took around 20 minutes last night creating this form as i ran into a few problems, this should work if not please contact me :)

- Dan

craigg.
21-11-2005, 11:18 AM
Nice! , I cant see any errors as far as my eye's. :D

Great Tut, Im sure many user's will use this.

Keep the tutorial Frenzy coming! ^_^

Dan Williamson
21-11-2005, 11:19 AM
LOL, i will do don't worry dude, i got all these saved on my desktop to put onto my site :)

- Dan

Steven.
21-11-2005, 11:22 AM
I see a new Tutorial forum coming "Raremandans Tutorial's" :rolleyes:

Thank's for the new Tut :-) another one i will use :'D

craigg.
21-11-2005, 11:28 AM
I Forgot about my old tutorial on here ^_^ You can use that on your site if you wish Its a bit out-dated now Rofl.

http://www.habboxforum.com/showthread.php?t=79006

:D

Peace Dudes/Dudettes

Dan Williamson
21-11-2005, 11:29 AM
Cheers lol :)

I'm happy writing them as long as people need them, As i love learning new things :)

- Dan

Matt.
21-11-2005, 04:20 PM
Very good ill be using it:) + rep:)
Edit: Iam very sorry but it says "You must spread some Reputation around before giving it to Raremandan again." sorry:(

Splinter
21-11-2005, 04:28 PM
To check if a variable is empty its better to use:


if (!$post) {
echo "You havent filled in this field";
}


also why do you keep ending the <? ?> tags :S to write something just use echo :P Its abit long winded but well construced.

Instead of using a meta redirection the easier way in php is to do:


header("Location: formsent.php");

Owen
21-11-2005, 05:07 PM
Just what i needed
But the test email thingys are taking ages to come >:l

[N]
21-11-2005, 05:21 PM
Hey keep it up :D

Dan Williamson
21-11-2005, 05:28 PM
To check if a variable is empty its better to use:


if (!$post) {
echo "You havent filled in this field";
}


also why do you keep ending the <? ?> tags :S to write something just use echo :P Its abit long winded but well construced.

Instead of using a meta redirection the easier way in php is to do:


header("Location: formsent.php");



Soz dude.

As i say i'm pretty new to PHP and i'll have to figure some of these things as i go :)

- Dan

Owen
21-11-2005, 05:48 PM
Doesn't seem to be working :s

Dan Williamson
21-11-2005, 05:49 PM
Whats the problem dude?

- Dan

Owen
21-11-2005, 05:50 PM
I sent a test email about an hour ago or summin and it still hasn't came ;[
The emails right and everything but meh

Dan Williamson
21-11-2005, 05:51 PM
I dunno, it worked for me, i got the email withing 5 minutes "/

- Dan

Splinter
21-11-2005, 06:35 PM
most likely a server error :D and dan your learing fast.

Dan Williamson
21-11-2005, 06:38 PM
most likely a server error :D and dan your learing fast.

Thanks dude.

*Patiently awaits Chillimagiks new version*

*Stomps on AX's head*

- Dan

COOLK1D4
24-11-2005, 04:49 AM
Thanks man i bet people will love this!!!

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