Form Handling in PHP

After that much knowledge of Theory Let’s Look at Example.If you find difficulty feel free to contact us.

Form Handling Example.

For that you must be aware of form tags and other html tags how they works. Take a look at karan.html file below :

<html>
<head>
<title>Welcome to kanulp’s Blog</title>
</head>
<body>
<form action=”gajjar.php” method=”post”>
<table border=0>

<tr>
<h1 aligh=center>Information:</h1>
<td>Name:</td>
<td><input type=”text” required=”required” name=”fname”
placeholder=”Karan Gajjar”></td>
</tr>

<tr>
<td>Email:</td>
<td><input type=”text” name=”email”
placeholder=”abc@example.com”></td>
</tr>

<tr>
<td>Age:</td>
<td><input type=”number” name=”age” size=3 required=”required”
maxlength=2></td>
</tr>
<tr>
<td>Interest:</td>
<td><select name=”lan”>

<option value=”php” name=”language”>PHP</option>
<option value=”C” name=”language”>C</option>
<option value=”python” name=”language”>Python</option>
<option value=”java” name=”language”>Java</option>
</select></td>
</tr>

<tr>
<td><input type=”radio” value=”Male” required=”required”
name=”sex”>Male</td>
<td><input type=”radio” value=”Female” required=”required”
name=”sex”>Female</td>
</tr>

<tr>
<td colspan=2><input type=”submit” value=”Submit”></td>
</tr>

</table>
</form>
</body>
</html>

This should be like:

php1

Now Let’s work with these Inputs to do so PHP helps us.

save another file as mention in form action attribute like mine is gajjar.php.

<html>
<head><title>Your information</title>
</head>
<body>
<h1>Information</h1>
<h3>Hi <?php echo $_POST[“fname”]?>,</h3>

<?php

$a = $_POST[‘age’];
$lang = $_POST[‘lan’];
$sex = $_POST[‘sex’];

if($lang==”C” && $a>16)
{
echo “Omg “.$_POST[“fname”].”! You Don’t know “.$lang.” at
the age of “.$a.”?”;
}
else if($lang==”php”){

if($sex==”Male”){
echo “So Mr. “.$_POST[“fname”].”. You are good to go with “.
$lang.” and This blog will help you Stay tuned!”;}

else{

echo “So Miss. “.$_POST[“fname”].”. You are good to go with
“.$lang.” and This blog will help you!”;}

}

else
{
echo “Wow! You found http://www.kanulp.wordpress.com at the age of
“.$a;
}

?>

</body>
</html>

How this works is with the help of $_POST[] . This helps us to collect form data.Same as you can use $_GET[] if you have set your method=”get” in html file.

Basically developers do not use get method as per security.you can see your data which you’ve entered in html file in URL when you click on submit.

REMEMBER: This is done through server only where PHP interpreter is set and executes our scripts.

Advertisements

Author: kanu_lp

Teck Geek guy... Bands Obsessed.

1 thought on “Form Handling in PHP”

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