Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | Form Processing and Business Logic - PHP

Chapter: Internet & World Wide Web HOW TO PROGRAM - Rich Internet Application Server Technologies - PHP

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Form Processing and Business Logic - PHP

Knowledge of a client’s execution environment is useful to system administrators who want to access client-specific information such as the client’s web browser, the server name or the data sent to the server by the client.

Form Processing and Business Logic


Superglobal Arrays


Knowledge of a client’s execution environment is useful to system administrators who want to access client-specific information such as the client’s web browser, the server name or the data sent to the server by the client. One way to obtain this data is by using a su-perglobal array. Superglobal arrays are associative arrays predefined by PHP that hold vari-ables acquired from user input, the environment or the web server, and are accessible in any variable scope. Some of PHP’s superglobal arrays are listed in Figure 23.11.


Superglobal arrays are useful for verifying user input. The arrays $_GET and $_POST retrieve information sent to the server by HTTP get and post requests, respectively, making it possible for a script to have access to this data when it loads another page. For example, if data entered by a user into a form is posted to a script, the $_POST array will contain all of this information in the new script. Thus, any information entered into the form can be accessed easily from a confirmation page, or a page that verifies whether fields have been entered correctly.


Variable name : Description


$_SERVER : Data about the currently running server.

$_ENV : Data about the client’s environment.

$_GET : Data sent to the server by a get request.

$_POST : Data sent to the server by a post request.

$_COOKIE : Data contained in cookies on the client’s computer.

$GLOBALS : Array containing all global variables.


Fig. 23.11 | Some useful superglobal arrays.


Using PHP to Process XHTML Forms

XHTML forms enable web pages to collect data from users and send it to a web server for processing. Such capabilities allow users to purchase products, request information, send and receive web-based e-mail, create profiles in online networking services and take advan-tage of various other online services. The XHTML form in Fig. 23.12 gathers information to add a user to a mailing list.


1     <?xml version = "1.0" encoding = "utf-8"?>


2     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"


3           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">


5     <!-- Fig. 23.12: form.html -->


6     <!-- XHTML form for gathering user input. -->


7     <html xmlns = "http://www.w3.org/1999/xhtml">


8           <head>


9                  <title>Sample form to take user input in XHTML</title>


10                 <style type = "text/css">


.prompt { color: blue;

12        font-family: sans-serif;

13        font-size: smaller }

14        </style>


15        </head>


16        <body>


17        <h1>Sample Registration Form</h1>


18        <p>Please fill in all fields and click Register.</p>


20        <!-- post form data to form.php -->


21        <form method = "post" action = "form.php">


22        <div>



23        <img src = "images/user.gif" alt = "User" /><br />

24        <span class = "prompt">

25                    Please fill out the fields below.<br />

26        </span>


28        <!-- create four text boxes for user input -->

29        <img src = "images/fname.gif" alt = "First Name" />

30                    <input type = "text" name = "fname" /><br />     


32        <img src = "images/lname.gif" alt = "Last Name" />

33                    <input type = "text" name = "lname" /><br />      


35        <img src = "images/email.gif" alt = "Email" />

36                    <input type = "text" name = "email" /><br />       


38        <img src = "images/phone.gif" alt = "Phone" />

39                    <input type = "text" name = "phone" /><br />     


41        <span style = "font-size: 10pt">

42                    Must be in the form (555)555-5555</span>

43        <br /><br />


45        <img src = "images/downloads.gif"

46        alt = "Publications" /><br />


48        <span class = "prompt">

49                    Which book would you like information about?

50        </span><br />


52        <!-- create drop-down list containing book names -->

53                    <select name = "book">     

54                    <option>Internet and WWW How to Program 4e</option>

55                    <option>C++ How to Program 6e</option>

56                    <option>Java How to Program 7e</option>

57                    <option>Visual Basic 2005 How to Program 3e</option>

58        </select>

59        <br /><br />


61        <img src = "images/os.gif" alt = "Operating System" />

62        <br /><span class = "prompt">

63                    Which operating system are you currently using?

64        <br /></span>


66        <!-- create five radio buttons -->

67                    <input type = "radio" name = "os" value = "Windows XP"       

68                    checked = "checked" /> Windows XP     

69        <input type = "radio" name = "os" value =

70                    "Windows Vista" /> Windows Vista<br />

71        <input type = "radio" name = "os" value =

72                    "Mac OS X" /> Mac OS X

73        <input type = "radio" name = "os" value = "Linux" /> Linux

74        <input type = "radio" name = "os" value = "Other" />

75                    Other<br />


77        <!-- create a submit button -->

78        <input type = "submit" value = "Register" />


79        </div>


80        </form>


81        </body>


82        </html>

Fig. 23.12 | XHTML form for gathering user input.


The form’s action attribute (line 21) indicates that when the user clicks the Register button, the form data will be posted to form.php (Fig. 23.13) for processing. Using method = "post" appends form data to the browser request that contains the protocol (i.e., HTTP) and the URL of the requested resource (specified by the action attribute). Scripts located on the web server’s machine can access the form data sent as part of the request.


We assign a unique name (e.g., email) to each of the form’s controls. When Register is clicked, each field’s name and value are sent to the web server. Script form.php accesses the value for each field through the superglobal array $_POST, which contains key/value pairs corresponding to name/value pairs for variables submitted through the form. [Note: The superglobal array $_GET would contain these key/value pairs if the form had been sub-mitted using the HTTP get method. In general, get is not as secure as post, because it appends the information directly to the URL, which is visible to the user.] Figure 23.13 processes the data posted by form.html and sends XHTML back to the client.

Function extract (line 29 in Fig. 23.13) creates a variable/value pair corresponding to each key/value pair in the associative array passed as an argument (i.e., $_POST). This creates variables whose respective names and values correspond to the names and values of each posted form field. For example, line 36 in Fig. 23.12 creates an XHTML text box with the name email. In line 70 of our PHP script (Fig. 23.13), after having called func-tion extract, we access the field’s value by using variable $email. Elements in $_POST can also be accessed using standard array notation. For example, we could have accessed the form field email’s value by referring to $_POST[ 'email' ].


1     <?php print( '<?xml version = "1.0" encoding = "utf-8"?>' ) ?>


2     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"


3           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">


5     <!-- Fig. 23.13: form.php -->


6     <!-- Process information sent from form.html. -->


7     <html xmlns = "http://www.w3.org/1999/xhtml">


8           <head>


9                  <title>Form Validation</title>


<style type = "text/css">

11        body   {           font-family: arial, sans-serif }

12        div       {           font-size: 10pt;

13                                text-align: center }

14        table   {           border: 0 }

15        td         {           padding-top: 2px;

16                                padding-bottom: 2px;

17                                padding-left: 10px;

18                                padding-right: 10px }

19        .error   {           color: red }

20        .distinct {        color: blue }

21        .name {           background-color: #ffffaa }

22        .email { background-color: #ffffbb }

23        .phone           { background-color: #ffffcc }

24        .os       { background-color: #ffffdd }

25        </style>


26        </head>


27        <body>


28        <?php


29        extract( $_POST );


31        // determine whether phone number is valid and print


32        // an error message if not


33        if ( !ereg( "^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$", $phone ) )

34        {



35        print( "<p><span      class = 'error'>          

36        Invalid phone           number</span><br  />

37        A valid phone number must be in the form

38        <strong>(555)555-5555</strong><br />

39        <span class = 'distinct'>     

40        Click the Back button, enter a       valid phone

41        number and resubmit.<br /><br />

42        Thank You.</span></p>" );           

43        die( "</body></html>" ); // terminate script execution

44        }


45        ?><!-- end PHP script -->


46        <p>Hi


47        <span class = "distinct">

48        <strong><?php print( "$fname" ); ?></strong>


49        </span>.


50        Thank you for completing the survey.<br />


51        You have been added to the


52        <span class = "distinct">

53        <strong><?php print( "$book " ); ?></strong>


54        </span>


55        mailing list.

56        </p>


57        <p><strong>The following information has been saved


58        in our database:</strong></p>


59        <table>

60        <tr>



61        <td class = "name">Name </td>

62        <td       class = "email">Email</td>

63        <td       class = "phone">Phone</td>

64        <td class = "os">OS</td>


65        </tr>

66        <tr>



67        <?php            

68        //          print each form field’s value

69        print( "<td>$fname $lname</td>

70                    <td>    $email</td>

71                    <td>$phone</td>

72                    <td>$os</td>" );

73        ?><!-- end PHP script -->


74        </tr>

75                 </table>


76                 <br /><br /><br />


77                 <div>This is only a sample form.


78                       You have not been added to a mailing list.</div>


79          </body>


80    </html>

Fig. 23.13 | Process information sent from form.html.


Line 33 determines whether the phone number entered by the user is valid. In this case, the phone number must begin with an opening parenthesis, followed by an area code, a closing parenthesis, an exchange, a hyphen and a line number. It is crucial to validate information that will be entered into databases or used in mailing lists. For example, val-idation can be used to ensure that credit card numbers contain the proper number of digits before the numbers are encrypted and sent to a merchant. This script implements the busi-ness logic, or business rules, of our application.

The expression \( matches the opening parenthesis of the phone number. We want to match the literal character (, so we escape its normal meaning by preceding it with the backslash character (\). This parenthesis in the expression must be followed by three digits ([0-9]{3}), a closing parenthesis, three more digits, a literal hyphen and four additional digits. Note that we use the ^ and $ symbols to ensure that no extra characters appear at either end of the string.


If the regular expression is matched, the phone number has a valid format, and an XHTML document is sent to the client that thanks the user for completing the form. Oth-erwise, the body of the if statement executes and displays an error message.


Function die (line 43) terminates script execution. This function is called if the user did not enter a correct telephone number, since we do not want to continue executing the rest of the script. The function’s optional argument is a string, which is printed as the script exits.

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.