Posted by Michael B Allen on 12/04/05 05:00
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>
Navigation:
[Reply to this message]
|