Ch-6 Input /Output
In Php, we
can give input at runtime by using HTML form,
So, first of
all we can create a form like:
<form >
<fieldset style="width:0px">
<legend >calculator</legend>
Enter First Number<input type ="text"
name="text1"><br>
Enter Second Number<input type ="text"
name="text2"><br>
<input type ="submit"
name="submit">
</fieldset>
</form>
</body>
</html>
Save this page as practice1.php
Output:
Enter First Number
Enter Second Number
Enter Second Number
If you want
to sum of these two number on bottom side similar page , so you have to modify third
line upper code as:
<form
action=" practice1.php " method="post">
And also,
for addition purpose you need to add php code at below side as:
<?php
$a=$_POST['text1'];
$b=$_POST['text2'];
echo
"addition of $a and $b is".($a+$b);
?>
So, finally
code will be
<html>
<body>
<form action=" practice2.php"
method="post">
<fieldset style="width:0px">
<legend >calculator</legend>
Enter First Number<input type ="text"
name="text1"><br>
Enter Second Number<input type ="text"
name="text2"><br>
<input type ="submit"
name="submit">
</fieldset>
</form>
</body>
</html>
<?php
$a=$_POST['text1'];
$b=$_POST['text2'];
echo "addition of $a and $b is".($a+$b);
save program as practice2.php
Output:
Enter First Number
Enter Second Number
Enter Second Number
addition of 78 and 56 is134
when, you write form action= “ practice2.php” it
means after submitting, form send input text value to similar page that is “practice2.php
“ .
if you want to see output to another
page , so you need to change page name by
form action eg.
form action= “ test.php”
then create
new page test.php
and code as:
<?php
echo "addition
of ". $_POST['text1']
," and
".$_POST['second'],"is <br>";
echo
($_POST['text1']+$_POST['second']);
?>
Save this page as test.php
Firstly ,run practice2.php .
Give input then press submit button.
Your page automatic turn by test2.php,
and show output.
Reason
to use method=post
Post method is use to transfer input
data securely, means data is invisible at transfer time.
We will discuss more information about
method in further chapter .
No comments:
Post a Comment