Forms and User Input continued

The sample page in the previous post contains two input fields that are name and age, when the user inputs the data and clicks on the submit button, the form’s data is sent to the file “welcome.php” file. The resulting php file would look something like this:



Welcome .

You are years old

The output of the script we made would look like:
Welcome Ernie
You are 35 years old.

That would be an overview of the full potential of the $_GET and $_POST variables and they will be discussed in depth later as we go deeper into their functionality in PHP scripts.

Functions that return Values

That’s how easy a function can be made to do a series of operations by being influenced by parameters. We now focus on the area of having functions that returns a value, say the processing of a block of code or an operation done on a set of data.

Sample :

function add($a,$b)
{
$total = $a + $b;
return $total;
}
echo "1 + 15 =" . add(1,15);
?>


The output of the code would be:

1 + 15 = 16

Next we focus on forms and user input as we go on the fascinating world of the powers of PHP as a scripting language.

PHP forms and User Input

PHP as a scripting language means it is designed to work with ease of use on web page designs. Such pages often require the user to input data which can then be treated as input to the various elements of the program that are contained within the script to obtain the desired result. The PHP $_Get and $_Post, are samples of variables that are used to obtain information from forms like the registration form on a membership page.

HTML forms and PHP work hand in hand together in such a way that any element in an HTML page is readily available to PHP scripts.

Sample Form:

Name :
Age :


Form Validation

As the previous post discussed, PHP is very easy to use with web pages for the language has all the elements of a web page as a useable element. Now, going forward, forms or pages are easily handled and checked on the user end, meaning when you open a page the data input is easier to verify on the user side rather than have the input sent back to the server for checking and then returning any errors should there be any. This reduces load on the server and network having the data validated on the user end, allowing the information to be sent when the form receives all correct input. This type of handling is called client side validation where the input page verifies itself only requesting for the next step in a process when it returns true (all entries are correct). This way, the error checking and return function is on the client side reducing network and server load.

PHP $_GET

The $_GET variable is an array of variable names with the corresponding values sent by the HTTP GET method. It is used to collect values from a form by using the method=”get”. All information sent by a form using this method is visible to all on the browser’s address bar but it is limited to only 100 characters.

Sample:

Name :
Age :

If the user clicks on the submit button, the address bar would look like the output below:

http://www.samplepage.com/welcome.php?name=Ernie&age=35

Take note that the variable contents and names are verbosely displayed so do not forget to take that into account when using the variable for forms processing.

Forms – Get Variable continued

The file “welcome.php” can now use the $_GET variable to obtain all the required information which is shown on the address bar. As it does that, all the name fields of the form will automatically be the ID keys in the $_GET array variable.

Welcome .

You are years old!

The use of the $_GET variable results in all data being shown in the address bar which would not be well suited for obtaining user names and passwords. The use of the said variable is practiced for it allows pinpoint bookmarking allowing specific pages (like the user’s information page) that can easily be located with the first few values. The limitation of 100 characters is somewhat limiting but it is very useful in some cases as we will show in future posts.

The $_Post Variable

The $_POST Variable
The variable is an array of variable names and values that is sent by the HTTP POST method. The $_POST variable is used to collect values from a form with the method=”post”. The information sent from a form through this method is invisible to others and has no limits with concerns to the amount of information to be sent.

Name :
Age :

This time, as the user clicks on the submit button, the address bar would not display any information looking like:

http://www.samplepage.com/welcome.php

Resizing images goes a long way

One of the most small but extremely important aspects of PHP programming that many programmers overlook is the failure to resize images. Of course when we say it’s common sense that websites with smaller images load faster than ones with bigger images, it’s as obvious as saying that the sky is blue.

However, since it is precisely assumed to be “common sense”, it is one of the aspects of PHP programming that is most often not given a moment’s notice. Simply upload the picture, and it’s all done. Which is a terrible disservice, actually, to the website you’re making. People who visit it will most likely be turned off my the long loading times, and with peoples attention span as it is, you’re bound to be shoved aside in favor of a faster loading site.

Zend – PHP to the Max

phpstackThe announcement of Sun that they are developing the Zend Platform, is set to revolutionize the internet which is ruled by PHP based applications. Web sites, apps and many more all use PHP as their main platform and what better way to boost it’s reach is the addition of it’s own proprietary platform that would allow developers to use it as if PHP was made for commercial or business applications. Much of the market that Microsoft still holds is on their business development platforms such as the .NET framework which is mandatory for all of their business applications such as those for financial and other uses. The boost would allow more stable use of the PHP language, maximizing the potential for more business. Sun Microsystems, one of the leading proponents of the Zend. It adds capabilities to other development platforms, yes even Microsoft’s to inter-communicate and work together combining the efforts of all the major developers in the world.

PHP 5.2.8. the safer version

Time to upgrade to PHP version 5.2.8. as PHP 5.2.7. is a security liability:

Due to a security bug found in the PHP 5.2.7 release, it has been removed from distribution. The bug affects configurations where magic_quotes_gpc is enabled, because it remains off even when set to on. In the meantime, use PHP 5.2.6 until PHP 5.2.8 is later released.

The PHP Development Team would like to announce the immediate availability of PHP 5.2.8. This release addresses a regression introduced by 5.2.7 in regard to the magic_quotes functionality, which was broken by an incorrect fix to the filter extension. All users who have upgraded to 5.2.7 are encouraged to upgrade to this release. Alternatively you can apply a work-around for the bug by changing “filter.default_flags=0? in php.ini.

Take out 5.2.7. as soon as possible and use the newest one for security’s sake!

Source