skip to Main Content

So I have a registration table in my website that has fields that need to be filled before submission. As far as I know, I have two options to make sure of that. First one is to use the ‘required’ attribute for each input or to check them at PHP level and using js. Which one is the better practice? Is there a better way to do it? And why?

Here is the way that I do it using HTML:

<form role="form" action="registration.php" method="post" id="login-form" autocomplete="off">
    <div class="form-group">
        <label for="username" class="sr-only">username*</label>
        <input type="text" name="username" id="username" class="form-control" placeholder="Enter Desired Username" required>
    </div>
      <div class="form-group">
        <label for="email" class="sr-only">Email*</label>
        <input type="email" name="email" id="email" class="form-control" placeholder="[email protected]" required>
    </div>
      <div class="form-group">
        <label for="password" class="sr-only">Password*</label>
        <input type="password" name="password" id="key" class="form-control" placeholder="Password" required>
    </div>

    <input type="submit" name="submit" id="btn-login" class="btn btn-custom btn-lg btn-block" value="Register">
</form>

In the PHP/JS version the code should look like this:

if (empty($username) || empty($email) || empty($password)){    
     echo "<script>alert('Fields cannot be empty')</script>";    
}

I appreciate your help.

3

Answers


  1. My suggestions is to have both, client & server side validation. That way you reduce server load & it’s good if you later turn it into the API for eg. Hope this helps.

    Login or Signup to reply.
  2. Client-side validation is the faster way to deal with the validation process than on the server-side because all the tasks happens on the webpage there itself and the network time form client to server is saved.

    But in only doing client-side validation there is a risk of attacks clients which can easily bypass the client-side so here it is need to validate the strings submitted by the cilent on the server-side which will save your data from the dangerous inputs.

    Note : In short, in terms of faster validation client-side is better and in terms of the security of the data server-side is a better option.

    Login or Signup to reply.
  3. The truth is the server is always out a limitation on every website so as all we can we have to reduce the computing and programs from server to Client-side and to lend the render to user’s computer. so if you can use js to do that never interfere PHP on this. just use js and if your problem is going to handle with pure CSS too is going to be very better.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search