Jump to content

Archived

This topic is now archived and is closed to further replies.

moonlightsonata

Empty field in an application

Recommended Posts

Im just starting in php so i hope you can help me guys...

How can i do this in php?
I have this fields that you need to fill up and theres a submit button

Now what i want to do is that if one of the fields was not fill up or is empty and
when the submit button is clicked you cannot proceed instead you will go back to the original application form(the fields) and the field that was not answered will be highlighted and will have an asterisk beside it while other fields who have answers will retain.

Just like when you are filling up or registering in yahoo for yahoo mail.
Thanks

Share this post


Link to post
Share on other sites
Here's one way
[code]
<?php
// initialise variables
$user = $pwd = '';

// set default field bg colors
$bgcol = array (
    'user' => '#FFFFFF',
    'pwd'  => '#FFFFFF'
    );
   
if (isset($_POST['user'])) {
    $user = $_POST['user'];
    if (!$user) $bgcol['user'] = '#FFCCCC';
   
    $pwd = $_POST['pwd'];
    if (!$pwd) $bgcol['pwd'] = '#FFCCCC';
   
}

// check that they were filled in
if ($user && $pwd) {

    // OK to process the input
    echo "User : $user<br>Password : $pwd<br>" ;
}

else {     
    // one or more is empty so display the form again
    echo "<form method='post'>\n";
    echo "Enter username ";
    echo "<input type='text' name='user' value='$user' style='background-color:{$bgcol['user']}'>";
    echo "<br>Enter password ";
    echo "<input type='text' name='pwd' value='$pwd' style='background-color:{$bgcol['pwd']}'>";

    echo "<br><input type='submit' name='submit' value='Submit'>";
    echo "</form>\n";
}
?>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.