PHP Lessons

How to insert data

Back

There are really two files: the first one is a HTML file that holds an input form, the second is the PHP file that processes the input data.

File calculate.htmFile calculate.php
<body>
<form action ="calculate.php" method="post">
Type in two numbers:
<input type=text name="a">
<input type=text name="b">
<input type="submit">
</form>
</body>
<body>
<?php
$sum=$a + $b;
$difference= $a - $b;
$product = $a *$b;
$ratio = $a/$b;
$remainder = $a%$b;

echo" <p>You have put in the numbers $a and $b</p>
<p>The sum of the numbers is $sum</p>
<p>The difference is $difference</p>
<p>The product is $product</p>
<p>The ratio is $ratio</p>
<p>The remainder of the division is $remainder</p>
";
?>
<p><a href="calculate.htm">Back</a></p>
</body>

As you can see, we used two input type text and one type submit inside a form tag.

The form tag must have two attributes (action and method) that allow the link to the calculate.php program.

It is absolutely indispensable the name attribute because the PHP program must create variables with the assigned name (in this case $a and $b) and with the values written by the user.

The script is a sequence of 5 assignment instructions and an echo instruction.

All the instructions end by ;

Until the 4.2 version PHP accepts without problems the variable names inserted by an input form.

You can use the variables inside the echo instruction (between quotation marks ", not '): in output the program will show the value of the variable.

Valid HTML 4.01 Transitional