On Sat, Dec 03, 2005 at 10:00:14PM -0500, Michael B Allen wrote:
> The following code works but I'm a little new to PHP and I'd like to
> know if there are better ways to achive the same thing.
> 
> In the below form example, if the user supplies one or more fields but
> not all that are required, the form is redisplayed but fields that were
> supplied are prepopulated and the ones that are required but are missing
> are highlighted in red.
> 
> Can anyone recommend a better technique or elighten me about features of
> the language that I've missed?
> 
> Mike
> 
> <html>
> <body>
> 

> <?php
>     $set = 0;
>     $username = "";
>     $password = "";
> 
>     if (isset($_POST['username'])) {
>         $username = trim($_POST['username']);
>         if (strlen($username) > 0) {
>             $set |= 0x01;
>         }       
>     }
>     if (isset($_POST['password'])) {
>         $password = trim($_POST['password']);
>         if (strlen($password) > 0) {
>             $set |= 0x02;
>         }       
>     }
>     if (($set & 0x03) == 0x03) { 
>         echo "Logging in with: " . $username . ':' . $password;
>     } else {
> ?>   
>      
> <form action="login.php" method="post">
> <table> 
> <tr><td colspan="2">Login:</td></tr>
> <tr><td>Username:</td>
>      
> <?php
>     if ($set != 0 && ($set & 0x01) == 0) { 
>         echo "<td bgcolor=\"#ff0000\">";
>     } else {
>         echo "<td>"; 
>     }
>     echo "<input type=\"username\" name=\"username\" value=\"" . $username . 
> "\"/>"; 
> ?>   
>      
> </td></tr>
> <tr><td>Password:</td>
>      
> <?php
>     if ($set != 0 && ($set & 0x02) == 0) { 
>         echo "<td bgcolor=\"#ff0000\">";
>     } else {
>         echo "<td>"; 
>     }
> ?>   
>      
> <input type="password" name="password"/>
> </td></tr>
> <tr><td colspan="2"><input type="submit" value="Login"/></td></tr>
> </table>
> </form> 

<style>
  .errored {
    color: #FF0000;
  }
</style>
<?php
     $set_class = array(0 => '', 1 => 'errored');
     define('SET_USERNAME', 0x01);
     define('SET_PASSWORD', 0x02);
     define('SET_ALL', SET_USERNAME & SET_PASSWORD);
     $set = 0;
     $username = "";
     $password = "";

     if (isset($_POST['username'])) {
         $username = trim($_POST['username']);
         if (strlen($username) > 0) {
             $set |= SET_USERNAME;
         }       
     }
> ...
     if (($set & SET_ALL) == SET_ALL) { 
         echo "Logging in with: " . $username . ':' . $password;
     } else {
 ?>   
<form action="login.php" method="post">
<table> 
<tr><td colspan="2">Login:</td></tr>
<tr><td>Username:</td>
<?php
  $class = $set_class[ $set & SET_USERNAME ];
  echo "<td class="$class">
  echo "<input type=\"username\" name=\"username\" value=\"" . $username . 
"\"/>"; 
> ...

It might be a bit overkill for such a simple thing.. but heck.. i
was bored :)

Curt.
-- 
cat .signature: No such file or directory

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to