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
LOCATION.php being the url of the form handler your using.HTML Code:<form name="form" id="form" method="post" action="LOCATION.php">
In older browsers rather than using the form handler you can just send it to your email. using the mailto tags
e.g.
Although in modern browsers this may just send a blank email because its no longer supported, by "most" browsers.HTML Code:<form name="form" id="form" method="post" action="mailto:[email protected]">
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
<input name="title" type="text" />HTML Code:<br> question :<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
Again each one must have its own Name.HTML Code:<textarea name="message" ></textarea>
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.
The options selectable are 1 to 5. you can ad and removed them in the same wayHTML Code:<select name="rate"> <option>5</option> <option>4</option> <option>3</option> <option>2</option> <option>1</option> </select>
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
Ad that to the end to make a submit button. Also change button text to the text you want to display on the button.HTML Code:<input type="submit" name="Submit" value="button text">
And to end the form. you just close the form tags.
HTML Code:</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
To tell the host machine, that it needs to do the php script.PHP Code:<?php
Then we start the form handler.
this makes sure that the information was submitted by the form. or at least a form.PHP Code:if ($_SERVER['REQUEST_METHOD'] != 'GET'){
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.
The above bit is getting the data form a input with the NAME messagePHP Code:$message = $_POST['message'];
and assigning it to the variable message.
. You need to do this for all the inputs
EG
Once all the forms data is in variables you then need to mail it to your email.PHP Code:$nam = $_POST['name'];
$email = $_POST['email'];
$mess = $_POST['message'];
$ip = $_POST['ip'];
the easiest way is be putting all the variables in to one variable
e.g.
NB: \n is a new line in phpPHP Code:$messig = "
Name: $nam
\n
Email: $email
\n
Message: $mess
\n
Ip: $ip
";
also you may want a subject for the email
Now you have the variable. you send itPHP Code:$subject = $nam;
with this bit of code.
The first part ois your email, or the email you want to messagePHP Code:mail("[email protected]", $subject, $messig);
$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
Now what to do is the message want send by a formPHP Code:Header("Location: thankyoupage.php");
Which just prints error is its viewed directly.PHP Code:}
else
{
echo 'ERROR';
}
And close the form surprisingly enough with
Getting and ipPHP Code:?>
To get an ip use this value
value="<?php echo $_SERVER['REMOTE_ADDR']; ?>"
so the full think would be
And if you don’t want the editing the fieldCode:<input name="ip" type="text" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>" />
just ad a readonly to the end
Keep in mind the contact file would have to be a .php one to be able to use this data :/HTML Code:<input name="ip" type="text" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>" readonly>
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.
Then linke an ordinary form it sends the option selected to the form handler.HTML Code:<select name="too"> <option>webdesigner</option> <option>moderators</option> <option>admin</option> <option>weeble</option> <option>bob</option> </select>
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
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 errorPHP 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"); }
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
Wich sustitutes an abosulte email, for an email we set to the $mailto variable.PHP Code:mail($mailto, $subject, $messig);
hope thats helps









Reply With Quote





