13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Reading Mail<br />

673<br />

value="index.php?action=select-account&account=3"><br />

localhost<br />

<br />

<br />

Most of this code is just an HTML select element, but it also includes a little<br />

JavaScript. In the same way that <strong>PHP</strong> can generate HTML, it can also be used to generate<br />

client-side scripts.<br />

Whenever a change event happens to this element, JavaScript sets window.location<br />

to the value of the option. If your user selects the first option in the select,<br />

window.location will be set to ‘index.php?action=select-account&account=10’.<br />

This results in this URL being loaded. Obviously, if the user has a browser that does not<br />

support JavaScript or has JavaScript disabled, this code will have no effect.<br />

The display_account_select() function, from output_fns.php, gets the available<br />

account list <strong>and</strong> displays the SELECT. It also uses the get_account_list() function discussed<br />

previously.<br />

Choosing one of the options in the SELECT activates the select_account event. If<br />

you look at the URL in Figure 29.5, you can see this event appended to the end of the<br />

URL, along with the account ID of the chosen account.<br />

Appending these GET variables has two effects. First, in the preprocessing stage of<br />

index.php, the chosen account is stored in the session variable $selected_account,as<br />

follows:<br />

case 'select-account':<br />

// if have chosen a valid account, store it as a session variable<br />

if(($account) && (account_exists($_SESSION['auth_user'],<br />

$account))) {<br />

$_SESSION['selected_account'] = $account;<br />

}<br />

break;<br />

Second, when the body stage of the script is executed, the following code is executed:<br />

case 'select-account':<br />

case 'view-mailbox':<br />

// if mailbox just chosen, or view mailbox chosen, show mailbox<br />

display_list($_SESSION['auth_user'],<br />

$_SESSION['selected_account']);<br />

break;<br />

As you can see, you take the same action here as if the user had chosen the View<br />

Mailbox option.We look at that action next.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!