Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Ch3

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

University College of Technology

Department of Computer Science

COURSE: PHP AND MYSQL


Chapter 3: Creating
Dynamic Website
Eng. Ismail Mohamed Jamal
MCSE (Master of Computer Science & Engineering)
Ondokuz Mayis University - Turkiye (OMU)
Email: engismailmj@gmail.com

1
Objectives

• After taking this chapter you should be able to:

 Including Multiple files

 Handling HTML forms Revisited

 Making Sticky Forms

 Creating your own Functions

2
Introduction

• This chapter introduces three new ideas, all commonly used to create
more sophisticated web applications.

• The first subject involves using external files, this is an important


concept as more complex sites often demand compartmentalization
some HTML or PHP code.

• Then we return to the subject of handling HTML forms, we will learn


some variations on this standard process.

• Finally, we’ll learn how to define and use our own functions.

3
Including Multiple Files

• PHP can readily make use of external files, a capability that allow us to divide our script and

websites into distant parts.

• Frequently, we will use external files to extract our HTML from our PHP or to separate out

commonly used process.

• PHP has four functions using external files:

1) Include ( )

2) Include_once ( )

3) Require ( )

4) Require_once ( )

4
Cont.

• To use them, our php script would have a line like:

include_once(‘filename.php’);

require_once (‘/path/to/filename.html’);

Using any one of these functions has the end result of taking all the
content of the included file and dropping it in parent script at that
juncture.

5
Cont.

Include It is possible to insert the content of one PHP file into another
PHP file (before the server executes it), with the include or require
statement.

include_once may be used in cases where the same file might be


included and evaluated more than once during a particular execution of
a script.

require_once statement is identical to require except PHP will check if


the file has already been included, and if so, not include (require) it
again.

6
andling HTML Forms, Revisited

A good portion of Chapter 2 “programming with PHP” involves


handling HTML forms with PHP.

All of those examples use two separator files:

One that displays the form and another that receives it.

While there’s certainly nothing wrong with this method, there


are advantages to putting the entire process into once script.

7
Cont.

To have one page both display and handle a form, a conditional must

check which action (display or handle) should be taken:

If ( /* form has been submitted */)


{
// handle it
}
Else
{
// display it
} 8
Cont.

To determine if the form has been submitted, check if a


$_POST variable is set (assuming that the form uses the post
method of course).

For example: create a hidden form input with a name of


submitted and any value.

<include type=“hidden” name =“submitted” value=“1”/>

9
Cont.

<html>
<body>

<form action="action.php">
First name: <input type="text" name="fname"><br>
<input type="hidden" name="country" value="Norway">
<input type="submit" value="Submit">
</form>

<p>Notice that the hidden field above is not shown to a


user.</p>

</body>
</html>

10
Cont.

• <html>
• <body>

• <form action="welcome.php" method="post">


• Name<input type="text" name="name">
• Email<input type="text" name="email">
• <input type="submit">
• </form>

• </body>
• </html>

11
Cont.

• <html>
• <body>

• Welcome Mr/Mss:<?php echo $_POST["name"];?><br>


• Your Email address is <?php echo $_POST["email"];?>

• </body>
• </html>

12
Making Sticky Forms

A sticky form is a standard HTML form that remembers how you filled it
out.

This is a particularly nice feature for end users, especially if you are
requiring them to resubmit a form after filling it out incorrectly in first
place.

To have PHP present that value print the appropriate variable into value
attribute of a control. e.g. :

<input type=“text” name=“city” size=“20” value=“<?php echo $city; ?>” />

13
Cont.

That is a nice example of the benefit of PHP’s HTML –


embedded nature: you can place PHP code anywhere,
including with in form elements.

-To present the status radio buttons or check boxes, add the
code checked=“checked” to their input tag.

Using PHP, you might write:

14
Cont.

- To present the value of a textarea, place the value


b/w the textarea tags:

<textarea name=“comments” rows=“40”


cols=“60”>

<?php echo $comments; ?> </textarea>

- To present a pull-down menu, add


selected=“selected” to the appropriate option.

15
Creating our own functions

PHP has the capability for us to define and use


our own functions for whatever purpose.

The syntax for making our functions is:

function function_name( ) {

// function code

16
ating function that takes arguments

A function can take any number of argument but the order in which
you list them is critical.

To allow for arguments, add variables to a function definition.

function writeMsg() {

echo "Hello world!";

writeMsg();

17
Any Questions?

18

You might also like