• No results found

Using Popup Windows

In document Web Programming With PHP and MySQL (Page 157-161)

This is a convenient place to introduce the topic of popup windows . Although they can potentially be used in conjunction with any web page, they can be particularly helpful when used to clarify the meaning of one or more of the questions in a web form.

Here is a typical example. The user is asked to agree to terms and conditions, but what are they? A link is provided so that he or she can fi nd out.

The system implementer needs to create a webpage containing the terms and conditions. We will assume that its relative address is "whatarethey.htm". Then a link has to be added to the HTML forming the web form next to the checkbox form object. This is likely to be

The target=_blank part of this indicates that clicking on the link will open a new webpage of the same size as the original window. Having read the information the user then needs to close the new page and continue completing the web form. This is potentially confusing and a distraction (generally of little practical value) from the user's main focus – completing the form.

An alternative is to arrange for a small window known as a popup window or just as a popup to appear, superimposed on the webpage with the necessary information.

The effect looks like this:

9 Passing Variables to a PHP Script I

Having read (or perhaps just glanced at) the terms and conditions, it is natural for the user to close the popup window, to clear it out of the way, and continue fi lling in the form.

There is no facility in HTML to create a popup window but we can do it using JavaScript. 3

Where the <a href="whatarethey.htm" target=_blank> What are they? </a> line would otherwise appear we place this complicated expression:

This is a JavaScript statement written as part of a HTML <a href> tag. Making this replacement has no effect on what the user sees on the web form, but clicking on the link now produces the new page as a popup.

Unfortunately the JavaScript used to generate the popup is far from intuitively obvious to write and is likely to prove hard to remember. This is where using a PHP function is helpful. We can place the JavaScript inside a PHP function with two arguments, the fi rst being the address of the new page and the second being the text of the link. A suitable PHP function would be the following:

3 JavaScript is a scripting language with some similarities to PHP. However it is generally used very differently, for example to check that a value entered in a web form is numeric, before it is sent to the destination page, or to change the colour of a link when the user's mouse moves over it. There are many books devoted to JavaScript (not to be confused with the programming language Java, which is entirely different). Our only use of JavaScript in this book will be in connection with popups.

150

We recommend placing standard functions such as popup together in one or more utility fi les, which are then included into your PHP scripts as they are needed.

If function popup is part of fi le utils.php, say, then to use it you should place the PHP instruction

in the fi le you use to generate the web form.

To create the popup from within the web form you need to put the PHP function call

at the appropriate place.

The extension of the page used to generate the web form will now need to be php, not htm or html, even if the rest of the lines in the fi les are HTML.

If there is no other PHP in the fi le the above two lines will need to be enclosed in PHP tags, e.g.

and

Now we have the popup function we can embellish it. For example we might make the width and height values into parameters which default to 500 and 250 respectively. A suitable revised version of the popup function would be like this:

9 Passing Variables to a PHP Script I

Chapter Summary

This chapter gives a detailed explanation of webforms as a means by which the user of a web browser can send information to a PHP script known as a destination page . It is shown how to specify form objects on a webform in HTML and how this process can often be simplifi ed using functions written in PHP.

Practical Exercise 9

(1) Convert the HTML used to create the webforms shown in Sects. 9.2 and 9.4 so that all the form objects are generated using PHP functions stored in 'utility' fi le wfutils.php.

(2) What effect does making the above change have on the names of the fi les needed to store the webforms?

153

© Springer International Publishing Switzerland 2015 M. Bramer, Web Programming with PHP and MySQL, DOI 10.1007/978-3-319-22659-0_10

Chapter 10

Passing Variables to a PHP Script II

Chapter Aims

After reading this chapter you should be able to:

• write the PHP statements that enable values sent by any of the form objects described in Chap. 9 to be used in a destination page

• send variable values to a destination page using an extended URL and write PHP statements to use the values sent in the destination page

• use session variables to pass values around the PHP scripts in a large website.

10.1 Introduction

In the last chapter we looked at the most common way of passing the values of variables into a PHP script: the use of a webform to send values from a sending page to a destination page written in PHP.

In this chapter we discuss the PHP statements needed in a destination page to make use of the values passed from a webform. We also discuss two other ways in which values can be passed into a PHP script.

In document Web Programming With PHP and MySQL (Page 157-161)