Form processing using PHP
Subject: Web development using PHP and MySQL
Create a simple form in the index.php page with the code:
index.php
Testing PHP
Note:
The method="post", this indicates the method we want the form to use in submitting its data, therefore we have to use the PHP $_POST special variable to access its data likewise.
The action="process.php" which tells the form where to post the data to, in this case a page name process.php, which we will create next and will contain some PHP code to handle and respond to the form.
Then name="country" this is actually the name of the textbox or text field, to respond or get data from to this textbox we will have to reference this name using the form post method like this $_POST[“country”].
process.php
Testing PHP
The PHP started with a comment line; recall a non-executable line that helps the programmer to describe what a program line of section is doing, for easy maintenance. As you can see in the program above that the first comment tell you what the line below is doing, which is to collect value from the “country” text field submitted with POST method and assign or store this value to the $val variable.
The print "You are from ". $val will then this will display a text along with the value of the variable.
Recall that PHP is loosely typed language, because we entered a text in this example then the $val variable stored the value as String, if we had entered a number let’s say 5 this same variable will behave like an Integer or we entered 10.2 it would handle it as a Double or Float.
Let us modify the process.php, to multiply the value of what we have in the $val variable by itself.
process.php
Testing PHP
We had simply use the multiplication (arithmetic) operator to multiply the variable $val by itself $val * $val and store the result it a new variable $val_mult.
The the next line does the display or output. To test this, enter a number into the text field in the form and submit.
By:
Benjamin Onuorah
Login to comment or ask question on this topic
Previous Topic Next Topic