PHP Form: A simple tutorial for PHP Form

· Dev & Design
Authors

Setting up a form in PHP script is exactly the same as in HTML


Setting Up Your Form

As in HTML, the form elements are enclosed within <form> tags.
Here is a small example.

<form action=”process.php” method=”post”>
Form elements and formatting etc.
</form<

The form element takes two values, First is the form’s “action” that tells to
which script the data has to be sent. In the example above it is “process.php”.
It can also be in the form of URL (e.g. http://www.vyom.co.in/scripts/private/processors/process.php)
Second is the “method” which tells the form how to send the data.
POST method sends data in data stream, and maintains it as a secret. like password
GET method is used to send data making visible to users. For example if we are sending
a name=george then it would look like (e.g. http://www.vyom.co.in/process.php?name=george)


Getting The Form Information

The variable that the form passes on by POST method is collected using $_POST.
In the following example it takes variable from the POST and assigns it to
the variable $name

$name=$_POST[‘variable’];

If we are using GET method to pass the data then should use the following code.

$variablename=$_GET[‘variable’];

For each variable that is passed from form, we should follow the above procedure.


How to create form to mail script

The following code is to create a system which will e-mail a user’s comments to you.

HTML
<form action=”mail.php” method=”post”>
Your Name: <input type=”text” name=”name”><br>
E-mail: <input type=”text” name = “email”><br><br>
Comments<br>
<textarea name=”comments”></textarea><br><br>
<input type=”submit” value=”Submit”>
</form>

The above code is used to make a simple form, where a user can enter their
e-mail address. name and comments if any. Here can add extra parts if required.
After adding remember to update the php script too.

PHP
<?
function checkOK($field)
{
if (eregi(“\r”,$field) || eregi(“\n”,$field)){
die(“Invalid Input!”);
}
}
$name=$_POST[‘name’];
checkOK($name);
$email=$_POST[’email’];
checkOK($email);
$comments=$_POST[‘comments’];
checkOK($comments);
$to=”php@vyom.co.in”;
$message=”$name just filled in your comments form. They said:\n$comments\n\nTheir e-mail address was: $email”;
if(mail($to,”Comments From Your Site”,$message,”From: $email\n”)) {
echo “Thanks for your comments.”;
} else {
echo “There was a problem sending the mail. Please check that you filled in the form
correctly.”;
}
?>

here we have used php@vyom.co.in, You should replace it with your own
e-mail address. save the script as mail.php and upload both the files.
And now just fill in your comment box.

First part of the script stops the spammers from using your form to send
their spam messages. this is done by checking for special characters not present
in the input that can be used to trick the computer into sending messages to other
addresses. It is this function which checks for these characters. and if they are found,
stops running the script.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s