PDA

View Full Version : Form Tutorial



Mentor
24-03-2005, 05:32 PM
Originally it was the answer to a question, but as its so long, and quite a few people get stuck with them, I decided to give it its own topic

This tutorial, goes through creating the basic form, and using php as a form handler. Hope it helps

The form itself only requires html

It should start with


<form name="form" id="form" method="post" action="LOCATION.php">

LOCATION.php being the url of the form handler your using.

In older browsers rather than using the form handler you can just send it to your email. using the mailto tags
e.g.


<form name="form" id="form" method="post" action="mailto:[email protected]">


Although in modern browsers this may just send a blank email because its no longer supported, by "most" browsers.
As well as this you don’t have as much control over how the data is displayed when send to you, or the ability to make a thank you page etc.


Next you need to put in some inputs.

for something like

Question: ______________

you would use



<br>
question :<input name="title" type="text" />

<input name="title" type="text" />
is what makes the text box itself. You can have as many as you like. but they must all have individual names so they can be told apart

eg
<input name="sheep" type="text" />
<input name="cow" type="text" />

etc. although its probably best if the name is something similar to the question. although its best to be one word, so interpreter doesn’t have any problems.

Another think you could have inside is a text box



<textarea name="message" ></textarea>

Again each one must have its own Name.

a text box is basically a bigger input. and multi line.

The only other major think you may want in a form is a multi choose input.



<select name="rate">
<option>5</option>
<option>4</option>
<option>3</option>
<option>2</option>
<option>1</option>
</select>

The options selectable are 1 to 5. you can ad and removed them in the same way
and again each one needs a new name.

Secondly, if you want an option, to say something but have a different value when submitted.
<option value="cow">1</option>
use that.
You can also use the value="" tag in the other boxes to have some text already written in to them.

To make a text aria invisible. which is mainly useful for php scripts but ill say anyway you simply change the type
from text
to hidden
eg
<input name="info" type="hidden" />
Or to make the letters invisible typed in to splodges (aka a password field)
use this
<input name="info" type="password" />

finally we need a submit button :D


<input type="submit" name="Submit" value="button text">

Ad that to the end to make a submit button. Also change button text to the text you want to display on the button.

And to end the form. you just close the form tags.



</form>




THE FORM HANDLER
The next part is the php. where you need to use two main functions for the most basic form handler. although ill also throw in some security measures to prevent spamming.

Ok here we go.

Make sure the page your using. is the one you set as teh form handler loction in the form

for example. LOCATION.php

as every php file it starts with


<?php


To tell the host machine, that it needs to do the php script.

Then we start the form handler.



if ($_SERVER['REQUEST_METHOD'] != 'GET'){


this makes sure that the information was submitted by the form. or at least a form.

Then we tell it what to do.

For every input text box, multiple choice question. In the form you will need this bit of code to set it as a variable, in order to email.



$message = $_POST['message'];

The above bit is getting the data form a input with the NAME message

and assigning it to the variable message.

. You need to do this for all the inputs

EG


$nam = $_POST['name'];
$email = $_POST['email'];
$mess = $_POST['message'];
$ip = $_POST['ip'];


Once all the forms data is in variables you then need to mail it to your email.

the easiest way is be putting all the variables in to one variable

e.g.


$messig = "

Name: $nam
\n
Email: $email
\n
Message: $mess
\n
Ip: $ip

";


NB: \n is a new line in php
also you may want a subject for the email


$subject = $nam;

Now you have the variable. you send it

with this bit of code.


mail("[email protected]", $subject, $messig);


The first part ois your email, or the email you want to message

$subject is the messages subject.

And the 3rd bit is the actual message, or the variable with the message you made.

You can send as much or as little data as you like.


Once you’ve sent the form. redirect them else where with


Header("Location: thankyoupage.php");


Now what to do is the message want send by a form



}
else
{
echo 'ERROR';
}


Which just prints error is its viewed directly.

And close the form surprisingly enough with



?>


Getting and ip


To get an ip use this value

value="<?php echo $_SERVER['REMOTE_ADDR']; ?>"

so the full think would be


<input name="ip" type="text" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>" />

And if you don’t want the editing the field
just ad a readonly to the end



<input name="ip" type="text" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>" readonly>


Keep in mind the contact file would have to be a .php one to be able to use this data :/

Hope this help. Please point out any mistakes. And I hope this helps people

Any questions, or relevant stuff I may ad to it


emailing a varible email adress

Basicaly all this needs is a form vaiable with in the actal send mail part.

Easyist way is to have a drop down menu including the emails, but the linkly hood is, no one would know who was who so it would be pointless.

ok lets start with the form.

(exsample)
say this is what we had in the form istelf.


<select name="too">
<option>webdesigner</option>
<option>moderators</option>
<option>admin</option>
<option>weeble</option>
<option>bob</option>
</select>


Then linke an ordinary form it sends the option selected to the form handler.

in the form handler, first of all you want to change the names from the input in to emails.
Im going to do this with an if stamentet, but before that it needs to be truned in to a varible

( stuff with // before them are comments, not part of the code


$tool = $_POST['too']; // now its a varible

//next we do the if stament
if($too == "webdesigner"){$mailto = "[email protected]" ;}
elseif($too == "moderator"){$mailto = "[email protected]" ;}
elseif($too == "admin"){$mailto = "[email protected]" ;}
elseif($too == "weeble"){$mailto = "[email protected]" ;}
elseif($too == "bob"){$mailto = "[email protected]" ;}
else {die("Error, no one to send email to"); }


The if stament just see's if the vaible, $too's data maches any of the possoiblitys, if it doesnt, then the die command, stops the script and outputs an error

lets say it worked. now we have a $mailto vaible containing who we want to send the email to. so isntd of the old mail function we would use this


mail($mailto, $subject, $messig);


Wich sustitutes an abosulte email, for an email we set to the $mailto variable.

hope thats helps

-JT-
24-03-2005, 06:06 PM
i would pin it but the pinned section looks a bit messy. cIts rly good btw.

Mentor
24-03-2005, 06:27 PM
i would pin it but the pinned section looks a bit messy. cIts rly good btw.
True and thx. The forum could probaly do with an extra forum, which any good toutrials or helpful staff can be moved to by staff :/
Anyway im probly gona modifee this for my own toutrial archivy thing when i get it working :/

-JT-
24-03-2005, 06:47 PM
Yeah, am i still helping with that? and i agree about the tuts, i will speak to admin about it.

Mentor
24-03-2005, 06:54 PM
Yeah, am i still helping with that? and i agree about the tuts, i will speak to admin about it.
Yup at least i hope so. Im currantly still figuring out how to make one, as alot of stuff needs to be drievn by multiple parts. I may be going to have to have my first go at makeing a script with mysql :)

-JT-
24-03-2005, 07:16 PM
That will be craazy

Homosexual
24-03-2005, 07:36 PM
I'm still working on the iframe thing!!! So it's hard =o

:Blob
24-03-2005, 07:47 PM
Lol :D Iframe is good lol

Homosexual
24-03-2005, 07:48 PM
It's cool-i-o =]

splintercell!
24-03-2005, 08:11 PM
Its good I can do It but its really useful :D Also you should add a tut section or just delte some of the older ones or the less informative ones.. Like mr..mitchs :D

Mentor
21-04-2005, 01:48 PM
People are rambling about forms so im giving this a little bump *wistles inccently*

Ps. tut systesm up, Ish, its teh layoust thingy thats casuing problems is i need 2 figure out how 2 do an upload system "/

Acidude
15-08-2005, 01:51 PM
Aaaagh mine refuses to work :(

The form itsself is at http://www.habbophunk.com/request.html. When I full it in it gives me some complicated message that I don't get :P

I've posted the code below. Wold you mind looking through and checking what the mistake is? :)

Thanks.




<?php
if ($_SERVER['REQUEST_METHOD'] != 'GET'){
$mess = $_POST['habbo'];
$mess = $_POST['type'];
$mess = $_POST['dj'];
$mess = $_POST['message'];


$messig = "

Habbo Name: $mess
\n
Message Type: $mess
\n
Current DJ: $mess
\n
Message: $mess

";

$subject = $habbo;

mail($mailto, $subject, $messig);


$tool = $_POST['dj']; // now its a varible

//next we do the if stament
if($dj == "DJ Frobberwob"){$mailto = "andygreen1000********.com" ;}
elseif($dj == "DJ Acid"){$mailto = "Ziffachan********.com" ;}
else {die("Error, no one to send email to"); }

Header("Location: requestconfirm.php");

}
else
{
echo 'ERROR';
}


?>


The censored part is our e-mails :P

:Woof
15-08-2005, 01:52 PM
whats the contact codes?

Tomm
15-08-2005, 01:57 PM
lol who bumped this its 5 months old

Mentor
15-08-2005, 04:28 PM
Aaaagh mine refuses to work :(

The form itsself is at http://www.habbophunk.com/request.html. When I full it in it gives me some complicated message that I don't get :P

I've posted the code below. Wold you mind looking through and checking what the mistake is? :)

Thanks.




<?php
if ($_SERVER['REQUEST_METHOD'] != 'GET'){
$mess = $_POST['habbo'];
$mess = $_POST['type'];
$mess = $_POST['dj'];
$mess = $_POST['message'];


$messig = "

Habbo Name: $mess
\n
Message Type: $mess
\n
Current DJ: $mess
\n
Message: $mess

";

$subject = $habbo;

mail($mailto, $subject, $messig);


$tool = $_POST['dj']; // now its a varible

//next we do the if stament
if($dj == "DJ Frobberwob"){$mailto = "andygreen1000********.com" ;}
elseif($dj == "DJ Acid"){$mailto = "Ziffachan********.com" ;}
else {die("Error, no one to send email to"); }

Header("Location: requestconfirm.php");

}
else
{
echo 'ERROR';
}


?>


The censored part is our e-mails :P


you seem to have amade a few mistakes in how php works by assigning many differnt things the same varible for exsmple.
i was planing to ad full code exsamples but they stupuidly disabled the edit function "/

anyway

try this



<?php
if ($_SERVER['REQUEST_METHOD'] != 'GET'){

$mess1 = $_POST['habbo'];
$mess2 = $_POST['type'];
$mess3 = $_POST['dj'];
$mess4 = $_POST['message'];


$messig = "

Habbo Name: $mess1
\n
Message Type: $mess2
\n
Current DJ: $mess3
\n
Message: $mess4

";

$subject = $mess1;




$tool = $_POST['dj']; // now its a varible

//next we do the if stament
if($mess3 == "DJ Frobberwob"){$mailto = "andygreen1000********.com" ;}
elseif($mess3 == "DJ Acid"){$mailto = "Ziffachan********.com" ;}
else {die("Error, no one to send email to"); }

mail($mailto, $subject, $messig);

Header("Location: requestconfirm.php");

}
else
{
echo 'ERROR';
}


?>


All avibles have the own unice varible to store to, plus the mail to varible in your own was after the mail function wich sends the email itself wich is most likly what was throwing up all the errors, hope that helps :D

Acidude
15-08-2005, 04:37 PM
YAYAYAYAYAY! :D

WH00T! IT WORKS!

Thanks dude!

*Gives rep*

Now all I need to sort out is the radio issues XD

SamMaddock
26-08-2005, 11:32 AM
I need something that will count how many letters are being typed in a letter box and when it reachs 144 it wont let you type any more, anyone know how to build one?

:Lemmings.
26-08-2005, 11:56 AM
Wow this tutorial was written ages ago :)

SamMaddock
26-08-2005, 11:59 AM
I know :)

Mentor
27-08-2005, 09:42 AM
I need something that will count how many letters are being typed in a letter box and when it reachs 144 it wont let you type any more, anyone know how to build one?
ok im still not a 100 what u mean.

u can limit amount of letters able to enter with the max lengh attribute


maxlength="144"

Or do u want javascript to activty type whats being entered.

Heres a quick code, im not great a javascript and this is off the top of my head so may not work


<script>
function CheckLength() {
MessageLength = document.FORMNAME.BITNAME.value.length;

Out = MessageLength - 144 ;

alert(Out + " charciters remaining");

}
</script>

Exsmaple of use



<script>
function CheckLength() {
MessageLength = document.FORMNAME.BITNAME.value.length;

Out = MessageLength - 144 ;

alert(Out + " charciters remaining");

}
</script>



<br>
<script>
CheckLength();
</script>
<form name="FORMNAME" method="post" action="">
<input type="text" name="BITNAME">
</form><br>

<a href='javascript:CheckLength()'>Check Post Length</a>

SamMaddock
03-09-2005, 10:01 AM
Back to the form handler...

Im creating a DJs message form, and perhaps there is a way or sending this infomation to a blank empty page instead. If so, can you send me the code and let me know if i need to change persmissions to the page im sending it to...

splintercell!
03-09-2005, 10:17 AM
you have to write it to a database first :)

Mentor
03-09-2005, 12:03 PM
indeed, youd want to probly write to a flat file database, wich is a little more work, for after adding to the db u then need to use a few functions to get the data back out again "/ i may ad a tutoral on that when i get time

SamMaddock
25-09-2005, 01:08 PM
I need it so that the application is sent from the email they typed in. Anyone?

Mentor
25-09-2005, 03:45 PM
Thats easy enogh
in the form function
mail("[email protected]", $subject, $messig);

you wnat to ad the from attrubuted

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

Then we want to replace the email with the email varibleentered so

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

Then change the final function for form to this, so the from attribute is added

mail("[email protected]", $subject, $messig, $from);


In final

$from = "From: ".$email." \r\n";
mail("[email protected]", $subject, $messig, $from);

ps. make sure $email contains the email varible they eneter

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