Home >>PHP Tutorial >PHP Display result in Textbox

PHP Display result in Textbox

How to display Result in textbox in PHP

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>
Output
Result:
Enter first number
Enter second number

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").

WAP Sum of two number and display the Result in third text box

<?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>
Output
Result:
Enter first number
Enter second number

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.

Fill Registration form, show Output in text box

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>
Output
Registration Form
Enter Your Name
Enter Your Email
Enter Your Password
Enter Your Mobile Number
Enter Your Address

Output.php

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>
Output
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.


Popular Tutorials

PHP Tutorial PHP Introduction For Beginners PHP Advantage PHP Software Requirement PHP WAMP Installation PHP XAMPP Installation PHP Syntax PHP Comments PHP Variables PHP $ and $$ Variables PHP Super global variables How to swap two numbers in PHP PHP Constant PHP:Magic Constant echo and print in PHP PHP Data Types PHP Form GET Method POST Method Form action PHP extract() Function PHP Display result in Textbox Multiple submit buttons php different actions Add two textbox values and display the result in third textbox PHP Operators PHP Arithmetic Operators PHP Assignment Operators PHP Comparative Operators PHP Logical Operators PHP Operators Precedence PHP Conditional Statement PHP if Statement PHP if-else Statement PHP if elseif else Statement PHP Switch Statement PHP calculator Profit and Loss What is Loop PHP For Loop Print star pattern in PHP Create table using for loop in PHP PHP While Loop PHP Do While Loop PHP Foreach Loop PHP String PHP String Functions PHP empty() function PHP str_split() Function PHP Numeric Functions PHP Tutorial PDF For Beginners PHP isset function PHP addcslashes() Function PHP addslashes() Function PHP bin2hex() Function PHP chop() Function PHP chr() Function PHP chunk_split() Function PHP convert_cyr_string() Function PHP convert_uudecode() Function PHP convert_uuencode() Function PHP count_chars() Function PHP crc32() Function PHP crypt() Function PHP echo() Function PHP explode() Function PHP fprintf() Function PHP get_html_translation_table() Function PHP hebrev() Function PHP hebrevc() Function PHP hex2bin() Function PHP html_entity_decode() Function PHP htmlentities() Function PHP htmlspecialchars() Function PHP htmlspecialchars_decode() Function PHP implode() Function PHP join() Function PHP lcfirst() Function PHP levenshtein() Function PHP localeconv() Function PHP ltrim() Function PHP md5() Function PHP md5_file() Function PHP metaphone() Function PHP money_format() Function PHP nl_langinfo() Function PHP nl2br() Function PHP number_format() Function PHP ord() Function PHP parse_str() Function PHP print() Function PHP printf() Function PHP quoted_printable_decode() Function PHP quoted_printable_encode() Function PHP quotemeta() Function PHP rtrim() Function PHP setlocale() Function PHP sha1() Function PHP sha1_file() Function PHP similar_text() Function PHP soundex() Function PHP sprintf() Function PHP sscanf() Function PHP str_getcsv() Function PHP str_ireplace() Function PHP str_pad() Function PHP str_repeat() Function PHP str_replace() Function PHP str_rot13() Function