Conceptual Explanation


The server computer recognizes ASP and PHP files from the extension to the filename. When a request from the client for a file named something.asp or something.php arrives at the server, the action is not simply to send that file over the network to the client, but to open and examine the file and process it. These files consist of a mixture of “straight” HTML and ASP or PHP code.

Delimiters for ASP and PHP

Delimiter symbols set off the ASP or PHP portions of the file. In the case of ASP, the first delimiter tag specifies JavaScript as the language, assuming you choose to use JavaScript, which is what will be done in this text. The default language for ASP is VBScript. An ASP file would resemble the following:

    <%@ language="JavaScript"%>     <html>  Other HTML      <%      JavaScript code with ASP      %>     </html>

You can go in to and out of ASP/JavaScript as many times as you want.

A PHP file would take the following form:

    <html>  Other HTML     <?php        PHP     ?>     </html>

(You can also use ASP style delimiters with PHP, but we will stick to the question marks to prevent confusion in the examples.)

Obtaining Form Data and Outputting HTML

The tasks of a server-side file, also commonly referred to as a script, are to access resources on the server and use the information to construct a customized HTML document to return to the client computer. One category of resources available on the server encompasses databases and files. The following chapters will cover databases; in this chapter, we stick to basic ASP/JavaScript and PHP. To generalize, to do ASP or PHP, you need to know how to use the appropriate programming language to perform the logic, and you need to know how to access and change resources.

When doing ASP, your code will include invocation of the methods of a small set of objects. The terms method and object both have specialized meanings in computing. Objects are encapsulations of data and procedures. The data is referred to as properties or attributes, and the procedures are called methods. For example, to obtain the value of a form input field called “first_name” when the form was submitted using the get method, you write:

    Request.QueryString("first_name")

The way to describe this using the language of objects is: QueryString is itself a collection of data within the object Request. The individual items within the query string are referenced using the name of the input tag in the original form.

Assume the person filling out the form entered the name “Aviva” and clicked the Submit button. This data is included as part of the query string. To display a formatted greeting, you would code:

   Response.Write("<h1>Hello, "+    Request.QueryString("first_name")+"</h1> <br>"); 

Reading and interpreting from the inside out, the Request.QueryString ("first_name") yields the string "Aviva". It is concatenated with other strings in front and in back, to produce: "<h1>Hello, Aviva</h1><br>". The Response object has a method named Write. This is the method (in the regular English meaning of the word method) to write out text to the HTML document to send to the client computer. The Write method of the Response object takes one argument, a string.

In the small fragment of ASP/JavaScript, the Response.Write and the Request.QueryString ("first_name") are ASP. The part that is JavaScript is the plus sign for the concatenation operator and the semicolon for ending the statement.

Most ASP/JavaScript scripts make use of variables. You, the programmer, set up a variable to hold a value. The following code fragment sets up and initializes a variable named first_name to be whatever was entered into the form. That variable is then used to output the customized HTML page:

    var first_name = Request.QueryString("first_name");     Response.Write("<ht>Hello, "+first_name+"</h1><br>");

The variable name could be anything you choose. We did not have to make it the same name as the name of the form input tag. However, having said that, you do need to be consistent. Once you choose a name, you need to stick to it when making references to the variable. The system will not understand something you might think of as simply a shorthand. For this reason, make the names something meaningful.

The PHP system includes the programming logic and built-in procedures and variables for accessing the resources on the server computer. For example, to output something to the HTML document to be sent to the client, you use either the function print or the function echo. The PHP language has many similarities to JavaScript, but there are differences. One of the most obvious differences with ASP is that variable names start with dollar signs. This requirement enables what can be called a special trick concerning character strings to be demonstrated later in the chapter. Another difference is that the operator for concatenation of string is a period and not a plus sign.

The people who install the PHP system on the server computer have choices to make concerning many of the built-in procedures. One choice concerns the way to access the form input values. The so-called long form is similar to ASP. However, a common choice is to allow a short form. When this choice is in place, the form input values are referenced by a dollar sign followed by the name of the tag. The PHP version of the example with the form input of a first name is:

    print("<h1>Hello, $first_name</h1><br>");

With the short form in place on the server, PHP treats variable names, form input using the get method, form input using the put method, and, to be covered in a later chapter, cookies all in the same straightforward way: a dollar sign followed by the name. It is not a big conceptual difference, but this feature along with the ability to embed these names within strings does tend to make the PHP scripts shorter than the ASP/JavaScript (or ASP/VBScript) scripts.

The PHP language does support concatenation. If you do need to concatenate two strings together, the PHP operator is a period.

ASP/JavaScript and PHP Programming

The ASP/JavaScript and PHP do have many technical features in common. JavaScript used “on the server” is essentially the same language as on the client, and PHP provides many of the features, often using very similar syntax. For example, each supports array variables. Remember: arrays are sets of data, indexed by numbers starting with zero. Both ASP/JavaScript and PHP provide features for checking a condition and executing code only if a condition is true. The basic feature for checking conditions is the if statement. It has the structure:

     if (condition) {          Some code      }

The condition could be a comparison: is the price greater than 10? If the condition is true, then the code, denoted as “Some code” here, is executed. If the condition is not true, then execution continues after the closing bracket. Another form of the if statement is:

     if (condition) {         code for true case     }     else {         code for false case     }

This performs in the expected way. The condition is evaluated. If that evaluation returns a true value, then the code of the true case is executed; otherwise, the code for the false case. The general syntax for PHP and ASP/JavaScript are the same. A brief aside: VBScript, the other language used with ASP, has a very similar feature. The terms are:

    If (condition) then      Code for the true case     Else      Code for the false case     End If

Both ASP/JavaScript and PHP have features for looping. Both have ways to bring in the material in other files. These and other features will be shown in the examples here and in the rest of the text.

Include File Facility

One important feature of both ASP and PHP is the ability to bring in other files. Dividing a problem into smaller problems is a well-established programming technique. In the simple quiz show application to be shown in the Examples section, you can put the question and answers in one file and make the files with the logic much more manageable. The PHP command for this is:

    include("statecapitals.php");

If this line of code is executed, then the file called statecapitals.php is brought into the script. Alternatively, you can use:

    require("statecapitals.php");

If you use this function, the statecapitals.php file will be brought in whether or not that line of code is reached.

The ASP version of this is:

    <!— #include file="statecapitals.asp"  —>

This expression is placed typically as the second line in the file, under the statement indicating the language choice. The include file is always brought into the script.

You can use any file extensions that you want for this. However, using PHP and ASP means that these files will not be shown by the browser if a player tries to look at the file; that is, the answers.

Random and Other Math Functions

Many computer applications, most notably games and quizzes, require the making of choices in an apparently random manner. For example, if you want to be quizzed on the states, you do not want to always start with Alabama. Among other things, you will probably never stay around to get asked about Wyoming. Most computer languages provide what are called pseudo-random functions. The “pseudo” is because the system uses a well-defined procedure to arrive at a result. There is no little man inside the computer flipping coins. For PHP, the function to be used in this chapter is:

    $choice=rand(0, 49); 

This statement assigns to the variable $choice the results of a call to the built-in function rand. When the built-in function is called with two arguments, it returns an integer (whole number) value from 0 to 49.

The ASP counterpart to this is:

    choice=Math.floor(Math.random()*50);

The function Math.random() returns a fraction greater or equal to zero and less than one. If you multiply such a value by 50, you get a number greater or equal to zero and less than 50 (but possibly greater than 49). The next step indicated in the expression is to take the floor of this value. The floor is the biggest whole number that is not more than its argument. The effect of this expression is to produce a whole number from zero to 49, exactly what we want.

The steps represented by this expression are shown in Table 4.1.

Table 4.1: Steps in Expression Calculating Choice

Expression

Result

Math.random()

Value from zero to [just] less than one

*50

Value from zero to [just] less than 50

Math.floor

Whole number value from zero to 49




Creating Database Web Applications with PHP and ASP
Creating Database Web Applications with PHP and ASP (Charles River Media Internet & Web Design)
ISBN: 1584502649
EAN: 2147483647
Year: 2005
Pages: 125
Authors: Jeanine Meyer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net