Wrapping PHP Applications in a Single Page


Say, for example, you want to use a single page to both get the user’s name and display that name. The text field in this example will be named “text,” so you might start by checking whether there is some data waiting for you under the name “text” (that is, the user has already entered his or her name) in this Web page, single.php:

 <html>     <head>         <title>           Putting everything in a single page         </title>     </head>     <body>         <center>           <h1>Putting everything in a single page</h1>           <?             if(isset($_REQUEST["name"])){           ?>           .           .           .         </center>     </body> </html>

If there is data waiting for you-that is, the user’s name-you can display it like this:

 <html>     <head>         <title>           Putting everything in a single page         </title>     </head>     <body>         <center>           <h1>Putting everything in a single page</h1>           <?             if(isset($_REQUEST["name"])){           ?>             Your name is           <?             echo $_REQUEST["name"];             }           .           .           .           ?>         </center>     </body> </html>

On the other hand, if there is no data waiting for you, you can display an HTML form to get the user’s name:

 <html>     <head>         <title>           Putting everything in a single page         </title>     </head>     <body>         <center>           <h1>Putting everything in a single page</h1>           <?             if(isset($_REQUEST["name"])){           ?>             Your name is           <?             echo $_REQUEST["name"];             }             else {           ?>             <form method="post" action="single.php">             .             .             .             </form>           <?             }           ?>         </center>     </body> </html>

Note 

And you could add the text field to that form, as well as a prompt to the user and a Submit button:

 <html>     <head>         <title>           Putting everything in a single page         </title>     </head>     <body>         <center>           <h1>Putting everything in a single page</h1>           <?             if(isset($_REQUEST["name"])){           ?>             Your name is           <?             echo $_REQUEST["name"];             }             else {           ?>             <form method="post" action="single.php">                 Enter your name:                 <input name="name" type="text">                 <br>                 <br>                 <input type="submit" value="Submit">             </form>           <?             }           ?>         </center>     </body> </html>

This page displays a text field if users haven’t already entered their name; if they have, it displays their name. You can see what this page, single.php, looks like in Figure 14.7, where it’s asking for your name.

image from book
Figure 14.7: The single.php page asks for your name.

After entering a name and clicking the Submit button, the same page tells you what name you entered, as shown in Figure 14.8.

image from book
Figure 14.8: The single.php page displays your name.

There you have it, no HTML opening page needed-everything was done with PHP.



Ajax Bible
Ajax Bible
ISBN: 0470102632
EAN: 2147483647
Year: 2004
Pages: 169

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