简体   繁体   English

检查 HTML 表中的单元格是否为空的最佳做法是什么?

[英]What is the best practice to check if the cells in an HTML table are empty?

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.第一个是对每个输入使用“必需”属性,或者在 PHP 级别并使用 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:这是我使用 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="somebody@example.com" 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:在 PHP/JS 版本中,代码应如下所示:

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

I appreciate your help.我感谢您的帮助。

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.这样你就可以减少服务器负载,如果你以后把它变成 API 就好了。 Hope this helps.希望这可以帮助。

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.但仅进行客户端验证存在攻击客户端的风险,这些客户端可以轻松绕过客户端,因此需要在服务器端验证 cilent 提交的字符串,这将从危险输入中保存您的数据.

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.注意:简而言之,就更快的验证而言,客户端是更好的选择,而就数据的安全性而言,服务器端是更好的选择。

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.因此,如果您可以使用 js 来做到这一点,那就永远不要干扰 PHP 。 just use js and if your problem is going to handle with pure CSS too is going to be very better.只需使用 js,如果您的问题将使用纯 CSS 来处理,也会变得更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM