Building an Online Job Application

Một phần của tài liệu Professional PHP Programming phần 2 docx (Trang 41 - 46)

In Chapter 4, we declared the constant COMPANY in jobapp.php. With require, this constant becomes much more useful, since it can be defined once in a common file and then accessed by any script that needs it. Let's create a new file called common.php to define a couple of constants we'll use over a number of pages:

<?php

// common.php

define ("COMPANY", "Phop's Bicycles");

define ("NL", "<BR>\n");

?>

Now we need to modify jobapp.php to make these constants available to it, and replace the literal values with our newly-defined constants:

<HTML>

<!-- jobapp.php -->

<BODY>

<?php

require ("common.php");

?>

<H1><?php echo (COMPANY); ?> Job Application</H1>

<P>Are you looking for an exciting career in the world of cyclery?

Look no further!

</P>

<FORM NAME='frmJobApp' METHOD=post ACTION="jobapp_action.php">

Please enter your name:

<INPUT NAME="applicant" TYPE="text"><BR>

Please enter your telephone number:

<INPUT NAME="phone" TYPE="text"><BR>

Please enter your E-mail address:

<INPUT NAME="email" TYPE="text"><BR>

Please select the type of position in which you are interested:

<SELECT NAME="position">

<OPTION VALUE="a">Accounting</OPTION>

<OPTION VALUE="b">Bicycle repair</OPTION>

<OPTION VALUE="h">Human resources</OPTION>

<OPTION VALUE="m">Management</OPTION>

<OPTION VALUE="s">Sales</OPTION>

</SELECT><BR>

Please select the country in which you would like to work:

<SELECT NAME="country">

<OPTION VALUE="ca">Canada</OPTION>

<OPTION VALUE="cr">Costa Rica</OPTION>

<OPTION VALUE="de">Germany</OPTION>

<OPTION VALUE="uk">United Kingdom</OPTION>

<OPTION VALUE="us">United States</OPTION>

</SELECT><BR>

<INPUT NAME="avail" TYPE="checkbox"> Available immediately<BR>

<INPUT NAME="enter" TYPE="submit" VALUE="Enter">

</FORM>

</BODY>

</HTML>

Let's suppose that we want jobapp_action.php to ask the user to confirm his or her input. We can start by adding an echo statement that simply shows some of the submitted data. While we're at it, we might as well also require our common file:

<HTML>

<!-- jobapp_action.php -->

<BODY>

<?php

require ("common.php");

echo ("<B>You have submitted the following:</B>" . NL . NL . // New line constant

"Name: $applicant" . NL . "Phone: $phone" . NL . "E-mail: $email" . NL);

// Convert to boolean $avail = isset ($avail);

echo ("Available immediately: " . ($avail ? "yes" : "no"));

?>

</BODY>

</HTML>

Next we will add two switch statements to check the two-letter country code (held in the $country variable) and the one-letter code for the position in which the applicant is interested. We will use the switch statements to determine the full names for the country and position to be displayed:

<HTML>

<!-- jobapp_action.php -->

<BODY>

<?php

require ("common.php");

echo ("<B>You have submitted the following:</B>" . NL . NL . // New line constant

"Name: $applicant" . NL . "Phone: $phone" . NL . "E-mail: $email" . NL . "Country: ");

switch ($country) { case "ca":

echo ("Canada");

break;

case "cr":

echo ("Costa Rica");

break;

case "de":

echo ("Germany");

break;

case "uk":

echo ("United Kingdom");

break;

default: // Must be "us"

echo ("United States");

}

echo (NL . "Position: ");

switch ($position) { case "a":

echo ("Accounting");

break;

case "b":

echo ("Bicycle Repair");

break;

case "h":

echo ("Human Resources");

break;

case "m":

echo ("Management");

break;

default: // Must be "s"

echo ("Sales");

}

echo (NL);

// Convert to boolean $avail = isset ($avail);

echo ("Available immediately: " . ($avail ? "yes" : "no"));

?>

</BODY>

</HTML>

The applicant will now be presented with a screen like the following when they have submitted their information:

Now that all of the submitted data is displayed, we can give the user the choice whether to submit them or cancel. We can do so by providing form buttons. The first will be used to submit the form; the second will return to the previous screen. For this, we use a bit of client-side DHTML, calling the back() method of the current window's history object:

// ...

// ...

// beginning of jobapp_action.php file

<FORM METHOD=post>

<INPUT TYPE="submit" VALUE="Submit">

<INPUT TYPE="button" VALUE="Go Back" ONCLICK="self.history.back();"

</FORM>

</BODY>

</HTML>

The experienced HTML programmer will notice that we have not yet set the ACTION attribute of this form. In other words, the Submit button will not really do anything yet. But it will. Oh yes, it will...

Summary

In this chapter, we have covered the structures that control the flow of code execution. The conditional statements, if and switch allow execution to differ depending on the values of expressions. PHP's switch statement is more flexible than many other languages' in that it allows any scalar expression to be used as a case label.

Loops facilitate repetitive tasks. PHP provides two basic types of loop: while and do...while loops are simple structures that continuously execute as long as a condition evaluates to true, whereas for loops offer a more convenient structure for loops in which a control variable is used to determine execution.

PHP provides two statements, require and include, which are used to incorporate code from outside files into a script. The require statement is replaced with the contents of the specified file, whereas the include statement evaluates and executes the code in the external file each time that it is encountered, making include more appropriate for use inside loops.

Finally, we looked at two other exection control statements: the break statement is used to halt the execution of a switch statement or a loop, and the exit statement is used to stop the entire script altogether.

7

Functions

A function is a block of code that can be defined once and then be invoked from other parts of the program. Typically, a function takes an argument or series of arguments, performs a predefined set of operations upon them and returns a resulting value. Functions allow you to write very modular, sensibly structured applications. Code that would otherwise be repeated often can instead exist in one place and be invoked (or called) throughout the rest of your program.

By writing and testing re-usable functions, you can save time and reduce the number of bugs in your code. PHP has numerous built-in functions, such as gettype() and isset(). In this chapter, we'll learn how to create our own user-defined functions.

Một phần của tài liệu Professional PHP Programming phần 2 docx (Trang 41 - 46)

Tải bản đầy đủ (PDF)

(86 trang)