Hi...

the easiest way I have found to do exactly what your looking for is to use a
pear package called html_quickform. I found it easy to use and it saves me
hours and hours of work too. If your interested go to
www.pear.php.net/html_quickform and have a look.



-----Original Message-----
From: Michael B Allen [mailto:[EMAIL PROTECTED]
Sent: Saturday, December 03, 2005 10:00 PM
To: php-general@lists.php.net
Subject: [PHP] How would you write this?


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>

<?php
    }
?>

<p/><a href="login.php">click me</a>

</body>
</html>

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

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

Reply via email to