Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

PHP handling form input and using SUPER GLOBAL VARIABLE

PHP handling form input and using SUPER GLOBAL VARIABLE

Handling form Input using PHP
Now you will learn how an application will work using form input.
Consider the following example
<?php
$a=10;
$b=20;
$res=$a+$b;
echo “Result=”.$res;
?>

Save this file as pro1.php and run. you will find same output like “Result=30”. If you want to change output, then will change the value of pro1.php file each time. So it is hectic job. Now you will solve this problem  throw form input. So first of all you need to create a input form using HTML
Create a  “project” directory in your root(htdocs  on xampp server or www on wamp server)

Save : add.html(in your project directory like “project”)
<html>
<head>
<title>Addition</title>
</head>
<body>
<form action=”sum.php” method=”post”>
Enter First No<input type=”text” name=”t1″><br>
Enter second No<input type=”text” name=”t2″><br>
<input type=”submit” value=”ADD”>
</form>
</body>
</html>

Now you will save sum.php  file in your “project” directory.

sum.php
<?php
$num1=$_POST[‘t1’];
$num2=$_POST[‘t2’];
$result=$num1+$num2;
echo”Result=”.$result;
?>

Now open bowser and type in url http://localhost/project/add.html
A form will appear on browser then enter value and submit the form using submit button. Now you will get result. here is one problem that you get result but now form is not appear . question is arise that why form is not appear .Answer is  when you submit the form, browser move on sum.php which is described in <form action=”sum.php”>in add.html file. To solve this problem now create a new file like addition.php and save both source code (php and html) in addition.php and try to run this program.

save: addition.php
<?php
$num1=$_POST[‘t1’];
$num2=$_POST[‘t2’];
$result=$num1+$num2;
echo”Result=”.$result;
?>
<html>
<head>
<title>Addition</title>
</head>
<body>
<form action=”addition.php” method=”post”>
Enter First No<input type=”text” name=”t1″><br>
Enter second No<input type=”text” name=”t2″><br>
<!– this part will replace in next example –><br>
<input type=”submit” value=”ADD”>
</form>
</body>
</html>

Now you will find output on same page. But How it possible:
Answer is-when you call addition.php and submit the value it call itself because
<form action=”addion.php”> have mentioned earlier .
Now focus on output you will find that the result is display above of form. it is not a correct way.
So again change some part of html in addition.php and run the program. After some changing result will appear on the form  .
Replace <!this part will replace in next example-> with following line
Result <input type=”text” value=”<?php echo $result;?>”>

Finally save: addition.php
<?php
$num1=$_POST[‘t1’];
$num2=$_POST[‘t2’];
$result=$num1+$num2;
echo”Result=”.$result;
?>
<html>
<head>
<title>Addition</title>
</head>
<body>
<form action=”addition.php” method=”post”>
Enter First No<input type=”text” name=”t1″><br>
Enter second No<input type=”text” name=”t2″><br>
Result<input type=”text” value=”<?php echo $result;?>”><br>
<input type=”submit” value=”ADD”>
</form>
</body>
</html>

In the above example you have seen that $_POST[‘t1’] or $_POST[‘t2’] are used. Actually  PHP has a rich kind of  SUPER GLOBAL VALIABLE ,That are following
$_POST[ ]
$_GET[ ]
$_REQUEST[ ]
$_SERVER[ ]
$_SESSION[ ]
$_COOKIE[ ]
$_FILES[ ]
$_ENV[ ]

In the above program form data are submitted using “post” so we use $_POST[]. $_POST[] is an array type variable which is associate with text field name “t1” and “t2”.If we use get method in form then value will be received using $_GET[] method. We can also use $_REQUEST method for both form data which is used “get” or “post”. Remaining SUPER GLOBAL VARIABLE will use one by one in next section

5 1 vote
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments