php表单验证
In previous articles I’ve looked at creating an accessible form using HTML. At the time I mentioned that there were two things we could not do, at least at that stage:
在以前的文章中,我研究了使用HTML创建可访问 表单 。 当时我提到至少在那个阶段我们不能做两件事:
Make a form look pretty (solved with CSS for forms)
- Actually do anything with the form information. 实际上使用表单信息做任何事情。
We can now solve the latter problem with PHP. But before we send the user’s submitted data anywhere, we must check that they have filled out our form correctly. This is known as “form validation”. (Not to be confused with HTML validation).
现在,我们可以使用PHP解决后一个问题。 但是,在将用户提交的数据发送到任何地方之前,我们必须检查他们是否正确填写了我们的表格。 这称为“表单验证”。 (不要与HTML验证混淆)。
On a professional website there are typically two “levels” of form validation. The first uses JavaScript or HTML5: as the user fills out the form, moving from one field to another, we check what they have entered, providing warnings on the page when entered data does not match the values we expect.
在专业网站上,通常有两个“级别”的表单验证。 第一种使用JavaScript或HTML5:当用户填写表单时,从一个字段移到另一个字段,我们检查他们输入的内容,并在输入的数据与我们期望的值不匹配时在页面上提供警告。
However, since both of these technologies are run client-side, and thus are both insecure and optional (i.e. the user can turn JavaScript off), it is typical to implement a second, server-side check of the data. This is usually done with PHP, and it is this part that we will do first.
但是,由于这两种技术都是在客户端运行的,因此都是不安全且可选的(即用户可以关闭JavaScript),因此通常需要对服务器进行第二次数据检查。 这通常是用PHP完成的,这是我们首先要做的部分。
There are three steps to validating a form with PHP:
使用PHP验证表单的步骤分为三个步骤:
Check that the entered data is correct. If it is, jump to step 3. Otherwise, go to step 2.
检查输入的数据是否正确 。 如果是,请跳至步骤3。否则,请跳至步骤2。
Display the form again, preserving any information entered this far and highlighting where errors occur. Allow the user to correct any mistakes and submit the form again, returning to step 1.
再次显示该表单,保留到目前为止输入的所有信息,并突出显示发生错误的位置。 允许用户纠正任何错误并再次提交表单,返回到步骤1。
When all form data is correct, process it (enter it into a database, send it in an email, etc), and provide some indication to the user that the process is complete.
翻译自: https://thenewcode.com/183/PHP-Form-Validation-Introduction
php表单验证