PHP for Beginners: Here is an example of how to create your own web page
Are you searching for PHP programs? Then you have landed on the right page.
PHP is a powerful language that is in graceful friendship with HTML. IT helps in making elegant and dynamic web sites that we see on the web today. Let us start by creating a simple page that will allow you to enter your order. The entire processing by the web page is the responsibility of the server-side languages including PHP. Let us conquer the process step by step and deep dive into the HTML and PHP codes that are worth helpful to implement this kind of dynamic web page.
Here is the necessary HTML code that is necessary to show to make the working of Php clear in this example.
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>BIG Company</title> </head> <body style="font-family: Palatino Linotype"> <h1><br> BIG Company</h1> <p>Component Order Form</p>
<FORM ACTION="process.php" method=post> <table border=1> <tr> <td width=152>Item<br> </td> <td width=16>Quantity<br> </td> </tr> <tr> <td>Silk</td> <td align="center"><input type="text" name="qtysilk" size="4" maxlength="4"></td> </tr> <tr> <td>Wool</td> <td align="center"><input type="text" name="qtywool" size="4" maxlength="4"></td> </tr> <tr> <td>Cotton</td> <td align="center"><input type="text" name="qtycotton" size="4" maxlength="4"></td> </tr> <tr> <td colspan="2" align="center"><br> <input type="submit" value="Enter Order"></td> </tr> </table> </form>
Now let’s try to understand the code. Most of the code as you see is ordinary HTML. Did you noticed that the ‘ACTION=’ on the above FORM statement points to a PHP file called process.php.
As soon as the user enters the necessary data and hits the “enter order” button, the file whose name or the URL appears in the ACTION statement will actually be loaded and the data the user typed into the form elements and will be passed to it. The data will be identified to that file by the field names used on the form, here “qtysilk”, “qtycotton” and “qtywool”. This is the main reason behind using easily recognized and meaningful names, in fact, good idea to use such as ours here.
This Html code was very necessary to show to understand the PHP code that processes the order.
Our order form sent its data to a file called process.php via the instruction:
This is the content of php file:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>BIG Company</title> </head> <body style="font-family: Palatino Linotype"> <h1><br> BIG Company</h1> <p> </p> <?php $qtySilk = $_POST['qtySilk']; $qtyWool = $_POST['qtyWool']; $qtyCotton = $_POST['qtycotton']; ?> <p>Thank you!</p> <p>Your order for:</p> <?php echo $qtySilk.' Silk<br>'; echo $qtyWool.' Wool<br>'; echo $qtyCotton.' Cotton<br>'; ?> <p>was processed.</p> </body> </html>
There are a lot of PHP code elements to look up here. We will start with looking at the important piece of the code first. $qtySilk = $_POST['qtySilk']; $qtyWool = $_POST['qtyWool']; $qtyCotton = $_POST['qtycotton'];
It might seem hard to look at but it is actually simpler than it looks. All the statements mentioned above are assignment statements or assignment instruction. These are the main part of any language you would like to learn. The use of the equal sign in the middle of the statement signifies making any value equal to the other value. Simply we are assigning or making the value of one variable equal to the other value.
A variable is a data element whose value can change depending upon situation and time whereas constants are the data elements that holds a particular or specific value during program execution and whose value does not change.
The variables on the left, are the variable fields and the above statements are dynamically defining them. Definition of the variables is also present in those statements and there is no need for the additional statements to define those variables.
These variables are set to the values of some variables “POST”. Variables “POST” it is those transmitted in the above manner using the POST method (remember the statement <form action = “process.php” method = post> on the form.) This format, using the names that were used in way call, you will collect the variables being transmitted.
Then there is that group of echo statements. “Eco” functions as a “print here” command. Remember that PHP processor will interpret the PHP code on this page and then send the page down to the client browser. They whatever echo prints out becomes part of the results page. Each of these states echoes prints the value of a variable and a text string in quotes. Look closely and you will see a period between the two – this tells PHP to concatenate the two strings (the value of the variable and the string in quotes) together.