Home >>PHP Tutorial >PHP Display result in Textbox
How to get input from HTML, do calculation and display output in text box.
Write a program to add two numbers and print the result in third text box.
To display the output in third text box there are many ways. First way : Take input from HTML make calculation and display the output in text box, for this use <input type="text" value="output"/> inside PHP script. like:
<?php if(isset($_POST['add'])) { $x=$_POST['fnum']; $y=$_POST['snum']; $sum=$x+$y; echo "Result:<input type='text' value='$sum'/>"; } ?> <body> <form method="post"> Enter first number <input type="text" name="fnum"/><hr/> Enter second number <input type="text" name="snum"/><hr/> <input type="submit" name="add" value="ADD"/> </form> </body>
In the given example,
First we design two textbox using HTML script with attribute name with ( value="Fnum" for first textbox) and (value= "Snum" for second textbox). A submit button with (name=Add).
When we run the program the logic that is defined inside PHP script, $_POST[ ] is used to collect the values from a form. it store the value in variables ($x,$y).
But we want to show the sum inside the textbox. for this we define textbox inside the "echo" statement with (value="$sum").
<?php $x=$_POST['fnum']; $y=$_POST['snum']; $sum=$x+$y; ?> <body> <form method="post"> Result <input type="text" value="<?php echo @$sum;?>"/><hr/> Enter first number <input type="text" name="fnum"/><hr/> Enter second number <input type="text" name="snum"/><hr/> <input type="submit" value="ADD"/> </form> </body>
In previous example,
the textbox is defined inside the PHP script. Instead this We define third textbox outside the PHP script.
Value attribute of <Input> tag is used to show the output inside the textbox here we define a PHP script inside the value Property of the textbox.
Create a Registration form fill all the information and display the result in text box on next page
Save the program as registration form.html
<html> <body> <form method="post" action="output.php"> <table bgcolor="#C4C4C4" align="center" width="380" border="0"> <tr> <td align="center"colspan="2"><font color="#0000FF" size="5">Registration Form</font></td> </tr> <tr> <td width="312"></td> <td width="172"> </td> </tr> <tr> <td><Enter Your Name </td> <td><input type="text" name="name" /></td> </tr> <tr> <td>Enter Your Email </td> <td><input type="email" name="email" /></td> </tr> <tr> <td>Enter Your Password </td> <td><input type="password" name="password" /></td> </tr> <tr> <td>Enter Your Mobile Number </td> <td><input type="number" name="num" /></td> </tr> <tr> <td>Enter Your Address </td> <td><textarea name="address"></textarea></td> </tr> <td align="center" colspan="2"><input type="submit" value="save" name="submit" /></td> </table> </form> </body> </html>
Registration Form | |
Enter Your Name | |
Enter Your Email | |
Enter Your Password | |
Enter Your Mobile Number | |
Enter Your Address | |
Save the program as output.php
<table bgcolor="#C4C4C4" align="center" width="380" border="0"> <tr> <td align="center"colspan="2"><font color="#0000FF">Your Output</font></td> </tr> <tr> <td>Your Name is</td> <td><input type="text" value="<?php echo $_POST['name']; ?>" readonly="" /></td> </tr> <tr> <td>Your Email is</td> <td><input type="email" value="<?php echo $_POST['email']; ?>" readonly="" /></td> </tr> <tr> <td>Your Password is</td> <td><input type="password" value="<?php echo $_POST['password']; ?>" readonly="" /></td> </tr> <tr> <td>Your Mobile Number is</td> <td><input type="number" value="<?php echo $_POST['num']; ?>" readonly="" /></td> </tr> <tr> <td>Your Address is</td> <td><textarea readonly="readonly"><?php echo $_POST['address'];?></textarea></td> </tr> </table>
Your Output will display like | |
Your Name is | |
Your Email is | |
Your Password is | |
Your Mobile Number is | |
Your Address is |
We create form using HTML script with (method="post") and action="output.php". We define Five fields inside the HTML form.
First for name,Second for Email_id, Third for Password,Fourth for Mobile No,and Fifth for address. A button is used to display data on next page after click on this button.
inside the (output.php) page we create same form format. But value attribute of every <input> type tag is used to declare the PHP script inside the value with $_POST[ ] .
$_POST[ ] is define with name of a field like. $_POST['name'], $_POST['email']. As we run this program the dada that is Entered in First page will dispaly as it is data on Second(output.php) page.