Wednesday 29 May 2013

Episode 23: Form validation using PHP



In this episode, I am trying to show how we can validate html form using PHP script. Form validation is very much important for web developed because without validation, an empty cell will be added to database and it is waste of memory in database. For this reason we construct validation to prevent add empty content to our database. Many times we need to create a login or registration page for the user and we need to all value of the form. In case users forgets or not fill up the field then there is a mistake of data which is needed. So we need to prevent that. Let do that,

At first we need a form like that,

          <form method=”post”>
          <input type=”text’ value=”” name = “first name” />
<input type=”text’ value=”” name = “last name” />
<input type=”submit’ value=”Ok” name = “Ok” />
</form>

Now create an php script for validation,

          <?php
         
if($_POST[‘Ok’])
{
          if(empty($_POST[‘first name’]))
          {
                   $msg = “Insert First Name !”;
}
if(empty($_POST[‘last name’]))
          {
                   $msg2 = “Insert Last Name !”;
}

Else
{
          Connect your database
And Store the value of field in database using Insert query.
}

}

?>

Here use a function empty ( ) which is building function of PHP for check that the field is empty or not. You can also use Javascript or Jquery for validation. Enjoy it.


Written by “Shojib”

No comments:

Post a Comment