PHP表单验证 [英] PHP Form Validation

查看:53
本文介绍了PHP表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

毫无疑问,这个问题很难以合理的方式回答和提出,但我会尽力而为:

This question will undoubtedly be difficult to answer and ask in a way that makes sense but I'll try my best:

我有一个使用PHP显示表单某些部分的表单,例如:

I have a form which uses PHP to display certain sections of the form such as:

<?php if ($_SESSION['EnrType'] == "Individual") { display only form information for individual enrollment } ?>

<?php if ($_SESSION['Num_Enrs'] > 6) { display only form information for 7 total members enrollment } ?>

在每个表单中,都会收集有关每个注册者的唯一信息,但是每个注册者的基本条件是相同的,即所有注册者必须使用的名字字段中都有一个值.每个字段均根据注册人编号来命名,即Num1FirstName;Num2FirstName.

In each form piece, unique information is collected about each enrollee but the basic criteria for each enrollee is the same, i.e. All enrollee's must use have a value in the FirstName field. Each field is named according to the enrollee number, i.e. Num1FirstName; Num2FirstName.

我有一个绝对不错的PHP验证脚本,并且不想更改它,但是我遇到的问题是该脚本的重复以便一次验证所有字段.

I have a PHP validation script which is absolutely fantastic and am not looking to change it but the issue I am running into is duplication of the script in order to validate ALL fields in one swoop.

提交后,所有POSTED项目都通过我的验证脚本运行,并且根据设置的规则,如果它们不等于true,则会返回错误.

On submission, all POSTED items are run through my validation script and based on the rules set return an error if they do not equal true.

示例代码:

if (isset($_POST['submit']))
{
  // import the validation library
  require("validation.php");

  $rules = array(); // stores the validation rules

  //All Enrollee Rules
  $rules[] = "required,Num1FirstName,Num2FirstName,The First Name field is required.";

上面的脚本执行以下操作,$ rules [] ="REQUIREMENT,fieldname,error message"要求给出条件(在这种情况下,只是传递一个值),fieldname是要验证的字段的名称,错误消息返回所使用的错误.

The script above does the following, $rules[] ="REQUIREMENT,fieldname,error message" where requirement gives criteria (in this case, simply that a value is passed), fieldname is the name of the field being validated, and error message returns the error used.

我的目标是使用与上面相同的公式,并使$ rules []遍历所有名字,并仅返回存在的错误(即如果屏幕上不存在,则不检查成员#7的名字).

My Goal is to use the same formula above and have $rules[] run through ALL firstnames and return the error posted ONLY if they exist (i.e. dont check for member #7's first name if it doesnt exist on the screen).

如果我只是在"fieldnames"之间加上一个逗号,那么它只会检查第一个,然后是第二个,依此类推,以此类推.

If I simply put a comma between the 'fieldnames' this only checks for the first, then second, and so on so this wont work.

有什么想法吗?

推荐答案

如果我了解您要正确执行的操作:

If I understand what you're trying to do correctly:

1.定义类型
首先,您需要定义哪种类型的字段需要哪种规则,例如:

1. Define types
First, you need to define what type of field requires what kinds of rules, e.g.:

$type['FirstName']['requirement'] = 'required';
$type['FirstName']['error_message'] = 'The First Name field is required.';
// just random example: $type['MobilePhone']['requirement'] = 'optional'; $type['MobilePhone']['error_message'] = 'Only enter digits.';

2.浏览每个发布的值
其次,检查每个过帐值是什么类型的字段.现在, $ _ POST 数组中可能包含很多东西,而您只需要检查某些字段即可.在输入字段中使用诸如 checkthis; 1234; FirstName 之类的名称可能会容易得多.

2. Go through each posted value
Second, check for each posted value what type of field it is. Now a lot of stuff might be included in the $_POST array, and you only need to check certain fields. It might make it a lot easier to use names like checkthis;1234;FirstName for your input fields.

foreach ($_POST as $key => $value) {
  // split the key, so you know what's what:
  $data = explode(';',$key);
// now $data[0] tells you what kind of field it is // $data[1] is the ID of the enrollee (your Num1, Num2) // $data[2] is the type of field
// only do checks for posted fields that start with "checkthis" if ($data[0]=='checkthis') { // now you can fill the $rule array: $rule[] = $type[$data[2]]['requirement'] . ',' . $key . ',' . $type[$data[2]]['error_message']; } }

这样,您在表单中包含的内容都没有关系.只要您定义了字段的类型,只要$ _POST值中包含规则,规则就会被添加到规则数组中,其余部分由验证脚本来完成.

This way it doesn't matter what you include in your form. As long as you've defined the type of field, a rule will be added to the rule array if it's included in the $_POST values, and your validation script will do the rest.

修改
如果您像这样构造输入字段:

Edit
If you structure your input fields like this:


<input type="text" name="F1Firstname" value="" />
<input type="text" name="F2Firstname" value="" />
etc..

..然后提交表单,$ _ POST数组将例如看起来像这样:

..and you submit the form, the $_POST array will e.g. look like this:

//print_r($_POST) gives:

Array
(
    [F1FirstName] => John
    [F2FirstName] => Jane
)

..最简单的方法是遍历每个使用 foreach 的用户:

..the easiest thing to do is to loop through each of those with foreach:

foreach ($_POST as $key => $value) {
  // first $key will be "F1FirstName"
  // first $value will be "John"

  // second $key will be "F2FirstName"
  // second $value will be "Jane"

  // etc
}

现在,$ _POST也将是其他类型的字段,例如 F1MobilePhone 或其他需要不同的验证和不同消息的内容.因此,对于这些字段中的每一个,您都需要找出它的类型,以确定要输入到验证功能中的消息类型.一种选择是(这在 foreach 语句中):

Now, in that $_POST will also be other types of fields, like e.g. F1MobilePhone or whatever, that require different validations and different messages. So for each of those fields, you need to find out what type it is, to determine what kind of message to enter into your validation function. One option would be (this goes within the foreach statement):

if (strstr($key,'FirstName')) {
  // add a validation rule for the FirstName type
}
if (strstr($key,'Somethingelse')) {
  // etc
}

我不知道您的验证脚本如何工作,但是我猜您正在执行以下操作:

I don't know how your validation script works, but I'm guessing you're doing something like this:

// import the validation library
require("validation.php");

$rules = array(); // stores the validation rules

// All Enrollee Rules
$rules[] = "required,Num1FirstName,The First Name field is required.";

// Call on the validation function
$result = validate($rules);

然后,在这种情况下,您可能会收到"Num1FirstName"是否有效的回信.由于您使用数组 $ rules ,因此验证程序可能可以一次处理多行.只需将更多的值添加到数组,而不添加其他变量.如果您这样做:

Then you'll probably receive back whether, in this case, "Num1FirstName" was valid or not. Since you use an array $rules, the validator can probably handle multiple lines at once. Simply add more values to the array, but don't add another variable. If you do this:

$rules[] = "required,Num1FirstName,Num2FirstName,The First Name field is required.";

..验证器将认为"Num2FirstName"是错误消息,因为这是您输入的第3个值,并且它不知道如何处理现在为第4个值的实际消息.尝试以下方法:

..the validator will think that the "Num2FirstName" is the error message, since that's the 3rd value you enter, and it won't know what to do with the actual message which is now 4th value. Try this instead:

$rules[] = "required,Num1FirstName,The First Name field is required.";
$rules[] = "required,Num2FirstName,The First Name field is required.";

因此,将所有这些结合在一起将为您提供:

So bringing that all together gives you this:

foreach ($_POST as $key => $value) {
  // first $key will be "F1FirstName"
  // first $value will be "John"

  // second $key will be "F2FirstName"
  // second $value will be "Jane"

  if (strstr($key,'FirstName')) {
    $rules[] = "required,$key,The First Name field is required.";
  }
  if (strstr($key,'Somethingelse')) {
    $rules[] = "required,$key,Some other message for this type of field.";
  }
}

// call on your validate function and feed it the $rules array you've created

在(预)验证中添加JavaScript是另外一回事.最后,您仍然需要在PHP中对其进行验证,因为就像您所说的那样,可以轻松地绕过JS检查.所以我想最好先解决这个问题.

Adding JavaScript to (pre-)validate is a whole different story. In the end you still need to validate it in PHP because, like you said, the JS check can easily be bypassed. So I guess fixing this first might be best.

这篇关于PHP表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆