Working with User Input


Let's expand that last example a bit. Say that you want the e-mail page not to display an e-mail address at all, but rather to collect some user information and do something with it (maybe store it in a database or send it as an e-mail). Here's the new page:

 <?php if (!eregi("modules.php", $_SERVER['PHP_SELF'])) {     die ("You can't access this file directly..."); } $module_name = basename(dirname(__FILE__)); $index = 1; function snailmail() {     global $module_name;     include("header.php");     OpenTable();     echo "Our address is:";     CloseTable();     include("footer.php"); } function email() {     global $module_name;     include("header.php");     OpenTable();     echo "<form action=\"modules.php?nameContactUs\" method=\"post\">";     echo "Your name: <input type=\"text\" size=\"20\" name=\"username\">";     echo "<input type=\"submit\" name=\"submit\">";     echo "<input type=\"hidden\" value=\"process\" name=\"which\">";     echo "</form>";     CloseTable();     include("footer.php"); } function process($username) {     global $module_name;     include("header.php");     OpenTable();       echo "You entered ".$username     CloseTable();     include("footer.php"); } function MainMenu() {     global $module_name;     include("header.php");     OpenTable();     echo "Which do you want?<br><br>";     echo "<ul>";     echo "<li><a href=\"modules.php?name=$module_name&amp;which=email\">E-Mail Address</a>";     echo "<li><a href=\"modules.php?name=$module_name&amp;which=mail\">Mailing Address</a>";     echo "</ul>";     CloseTable();     include("footer.php"); } switch($which) {     default:     MainMenu();     break;     case "mail":     snailmail();     break;     case "email":     email();     break;     case "process":     process($username);     break; } ?> 

Now, the email() function is displaying an HTML form. Here are the important elements of that:

  • The form's action sends it right back to this same module.

  • A text input box named username is collecting the user's name.

  • A hidden field named which contains the value process. This takes the place of a URL containing &which=process.

Now check out the switch construct at the end. A new case has been added to deal with the which=process possibility. This situation calls the process() function, passing a variable named $username. Notice that this variable name corresponds exactly with the input box name, username. That's how PHP-Nuke makes the match: Whatever the user typed is passed in the variable $username.

The process() function accepts $username and then displays its output, which makes use of that variable. Of course, you could write the process() function to send an e-mail, store something in a database, or whatever you like. That's a bit beyond the scope of this book, however.



    PHP-Nuke Garage
    PHP-Nuke Garage
    ISBN: 0131855166
    EAN: 2147483647
    Year: 2006
    Pages: 235
    Authors: Don Jones

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