Ten Minutes to PHP |
Written by Ian Elliot | ||||
Thursday, 16 September 2021 | ||||
Page 2 of 3
User interactionThere is obviously a lot more to the PHP language. It has arrays, pointer, objects, methods and inheritance, all of which deserve a large chunk of any article dealing with PHP, but none of these are the essence of web programming with PHP. So, at the risk of not explaining everything worthwhile about PHP, I’m going to show you what makes it useful. The key fact in using any server-side scripting language is interaction. You can use PHP to build custom web pages before they are sent to the client but the only reason for wanting to do this is if you know something about the client that makes you want to customise the page! For example, there is no point in trying to put together a personal greetings page unless you know who the user is. There are many ways of keeping track of a user, and global and environmental variables are the most primitive – but the place to start. Every web server maintains a set of values in a global array that provide information on the web browser being used and other useful information. A PHP program can access these values very easily as long as you know the name of the item you want. For example: <?php will display long string that includes the name of the browser in use. There are a number of similar global arrays that you can use for other types of data including a $GLOBAL array that stores all of the global variables that you can access. PHP arrays are “associative”; that is, values are associated with keys which in this case are also the names of the individual global variables. You can step though every key and value in an associative array using the special for loop:
foreach(array as $key=>$value){} This sets the $key variable and the $value variable equal to each key and value pair in turn. Using this you can now list every $_SERVER element, both its name and value: <html> As well as showing how to list all the server values this is also a good illustration of how powerful associative arrays are! You can also use foreach(array as $value) to just iterate through the values stored in an array. FormsPHP form handling works via the standard Get and Post methods. It is easy to define a web page which has text boxes and buttons, but the big problem generally speaking is how to deal with the data. In other words how to arrange for the data to be processed on the server. In the standard CGI (Common Gateway Interface) mechanism when the submit button is pressed either a program or another web page is loaded. For example, if you include the following HTML in a page: <form action=process.php method="GET"> a text field called “user” and a submit button called “B1” are created. When the user clicks the submit button the data in the text box is sent to the server and the web page or program listed in the “action” field is loaded. In this case the action field specifies a PHP web page and any PHP script contained within it is run by the server when the page is sent to the client. Exactly how the data is sent to the program depends on the “method” field – either GET or POST. The data from either a GET or POST is made available to the processing program in either of the arrays $_POST or $_GET. For example, to simply send back a web page that greets the user you might well use something as simple as: <html> Notice that this page would be saved as “process.php” and it’s the page loaded in response to the submit button being pressed. To retrieve the values from the form you would use the global $_GET array which stores all of the form data as an associative array. For example: <html> If you run this in connection with the form shown earlier it produces: user=Name Where Name is whatever the user entered before clicking the submit button. Of course you can use the usual techniques to keep the information that the user has supplied to you via the form. PHP has file-handling commands so you can write the data to disk in whatever format you care to use. If you want to use a database to store and retrieve data then this is relatively easy as well. You can use the same facilities to read data from disk and present it as an initialisation in a form. For example, if the variable $name is read in from a file then you could initialise the form using something like: <input type="text" size="20" value= This uses the simple PHP print to set the “value” attribute to the name of the user before the form is sent. The only possible confusion is the way the <?php and ?> brackets have to be within the <input > tag. There is an ugly shortcut version that might make this slightly more readable in that you can write: <?=$name?> in place of <?php print $name ?> Notice that the whole subject of how to mix HTML and PHP is a matter of great debate with many programmers stating that you shouldn't do it. Write a pure PHP program that generates the HTML. <ASIN:0596157134> <ASIN:1593271735> |
||||
Last Updated ( Thursday, 16 September 2021 ) |