Jump to content

Empty field in an application


moonlightsonata

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
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.