Welcome to YLOAN.COM
yloan.com » Jobs search » Working with Forms
Environment Relationships Religion and Faith Jobs search Economics Society residential christian

Working with Forms

Beginning Web Development for Smartphones: Developing Web Applications with PHP

, MySQL and jQTouch

Working with Forms

Forms are indispensible for acquiring user feedback or information. A form consists of input fieldstext, checkboxes, radio buttons, and so on. The data entered in these fields is passed on to a specified script for further processing after the Submit button has been clicked. The script can be any appropriate language, such as PHP, ASP.NET, or Python. In this book, we'll be using PHP as the server-side scripting language.

Here's an example of a form:


Submit

Input text fields are assigned unique namesUserid, ContactNo, and EmailAddressbecause data entered in the input fields is accessed by name only.

The attribute action points to a PHP file called createuser.php. The attribute method is set to POST, which means that the data entered into the input fields will be submitted to the createuser.php script using theHTTP request method POST for further processing. Form contents can be submitted to the specified script using either the GET or POST HTTP request methods.

GETThis method of passing form information is considered less secure than POST, as the data is actually displayed in the browser's address bar, passed along with the URL of the script. For example, let's assume that the PHP script to which we want to pass form's information is createuser.php, and the names assigned to the input fields are Userid, ContactNo, and EmailAddress. When the Submit button is taped, the browser's address bar would be:

crreateuser.php? Userid=John123&ContactNo=123456789&EmailAddress=johny123@gmail.com

Moreover, there is a limit on the amount of information passed through the GET method.

POSTThis method is usually preferred because the information it is not displayed in the browser's address bar and can handle larger amounts of data.

Note: The information sent by a form through GET and POST methods is stored in the associative arrays $_GET and $_POST respectively.

Collecting Information Sent by a Form

Recall that the two HTTP request methods by which information of a form can be passed to a target PHP script are GET and POST. We also noted that the information is then stored in the associative arrays, $_GET and $_POST. The target PHP uses these associative arrays to retrieve the form information, so let's talk a bit about them.

$_GET array

The $_GET array collects the values sent from a form using the HTTP GET method. When the user clicks the Submit button, all the information entered by the user in the form's input fields form is stored in $_GET. The associative array stores the information in the form of key/value pairs. The keys are the names assigned to the form's input text fields and the values are the data entered by the user in the respective keys. Thus, the data in the $_GET array can be accessed by specifying the key names. For example, information stored in the Userid key is accessed via $_GET["Userid"].

The target PHP script createuser.php can extract the data from a $_GET array using following code:

Note The period (.) in the echo command is used for concatenation. The trim() function truncates trailing spaces.

In this PHP script, we are using $_GET array to retrieve the information entered by the user in the form's input text fields Userid, ContactNo and EmailAddress, and store them into the key variables $uid, $contactno, and $emailid respectively. The information stored in the key variables then displayed on the screen via the echo command.

$_POST array

The $_POST array collects the values sent from a form that is using the HTTP POST method. When the user clicks the Submit button, all the posted information will be stored in the $_POST associative array in the form of key/value pairs . The keys are the names assigned to the form's input text fields. Information in the $_POST array is accessed by specifying the respective key. For example, to access the information stored in the Userid input field, we would use $_POST["Userid"].

The target PHP script createuser.php extracts the data from $_POST array and displays a welcome message in this bit of code:

$_REQUEST array

The $_REQUEST array contains the contents of both $_GET and $_POST. That is, the array is used to collect information from a form that is sending data by either the GET or POST method. If don't know which HTTP is being used by the form, it's a good idea to use a $_REQUEST array.

In the following code fragment, the target PHP script, createuser.php, extracts the data from a $ REQUEST array and displays a message to the user.

When a user enters information in the form and selects the Submit button, the data in the input text fields is passed to the createuser.php PHP script (see Figure 1 (a)). We are using $_REQUEST array in the PHP script to retrieve the user-entered information, Userid, ContactNo and EmailAddress, and store it into the variables $uid, $contactno and $emailid respectively. The code also creates a Welcome panel containing a toolbar with a Back button and a welcome message, displayed in the panel body via the echo command, as shown in Figure 1 (b)

Note: When user selects the Submit button, the application automatically jumps from the current panel to the target PHP script. We don't need to explicitly use a goTo function.


Figure 1 (a) Information entered in the form's input fields (b)Welcomepanel with toolbar and account confirmation message

For more information, refer to my book: "Beginning Web Development for Smartphones: Developing Web Applications with PHP, MySQL and jQTouch" available athttp://www.amazon.com/Beginning-Web-Development-Smartphones-ebook/dp/B0042X9CHI/ref=sr_1_2?ie=UTF8&m=AH9CGK6QR37LL&s=digital-text&qid=1284780488&sr=8-2

Working with Forms

By: B.M. Harwani
What Are Research Papers Is Your Job Killing Your Back? Shallotte Chiropractor Explains Advantageous Professional Link Building Services Massage Therapists: Ease Cramps More Effectively with the Help of a Professional Working And Reasons To Buy Phentramin- D Diet Pills Creating An Excellent Cover Letter For Your Job Hunting Massage Therapists: Ease Hurts More Effectively with the Help of a Professional Physical Therapist Jobs – A Wealth of Opportunity The Cam Model Profession Is Unlike Any Other, These Pros Earn 5X - 10X what other careers do U Can 2 Always seek Professional RAID recovery service to avoid permanent data loss Benefits Of Hiring Professional Movers And Packers Portable Job Career Options Professions That Demand Plastic Surgery
print
www.yloan.com guest:  register | login | search IP(216.73.216.142) California / Anaheim Processed in 0.016465 second(s), 7 queries , Gzip enabled , discuz 5.5 through PHP 8.3.9 , debug code: 60 , 6608, 91,
Working with Forms Anaheim