Posted by Stephen Leaf on 12/04/05 08:30
I personally would use javascript to evaluate the form and highlight the
fields onsubmit.
However as a backup I'd do the evaluating in php and add an error label under
the field.
echo "<td";
if ($noPass) echo " color='red'";
echo ">Password</td>";
if ($noPass) echo "<td color='red'>You must supply a password</td>";
Something along those lines.
On Saturday 03 December 2005 21:00, 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>
>
> <?php
> }
> ?>
>
> <p/><a href="login.php">click me</a>
>
> </body>
> </html>
[Back to original message]
|