简体   繁体   English

在PHP中验证表单输入

[英]Validating form input in PHP

is it possible to write a PHP page say form.php with a php function generalValidate($form) that is able to perform the following scenario : 有没有可能写一个PHP页面说form.php与php函数generalValidate($ form)能够执行以下场景:

  1. user browse to form.php 用户浏览到form.php
  2. user gets an html form 用户获取HTML表单
  3. user fills form and the form is sent back with POST method to form.php 用户填写表单,然后使用POST方法将表单发送回form.php
  4. form.php activate generalValidate($form) where form is the just recived form filled by user form.php激活generalValidate($ form),其中form是由用户填写的刚收到的表单
  5. generalValidate($form) returns true if this form is valid (for exemple properly filled), false else. 如果此表单有效(例如正确填充),则generalValidate($ form)返回true,否则返回false。

i think that another way to describe my question will be - is there a general way to iterate over an HTML form sent by a client (perhaps a form that the php page itself sent to client in the first place) while activating some code over each of the form values? 我认为另一种描述我的问题的方法是 - 是否有一种通用的方法来迭代客户端发送的HTML表单(可能是php页面本身首先发送给客户端的表单),同时激活一些代码。形式值?

dont eat me if its a bad question, im really just trying to learn :) 如果它是一个糟糕的问题,不要吃我,我真的只是想学习:)

a code exemple to fill for your convinience : 代码例如填写您的便利:

<?php
    function generalValidate($form) {
        ...
    }
    if (!isset($_SESSION)) 
            session_start();
    else if (generalValidate(...)) {

    }


?>
<html>
   <head>

   </head>
   <div>
      <p>Register</p>
   </div>
   <form id="regfrm" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" align="center">
      <table align="center">
         <tr>
            <td>First Name :</td>
            <td><input name="fname"  value="<?php if (isset($_POST["fname"])) {echo  v($_POST["fname"]);}?>" required></input></td>
         </tr>
         <tr>
            <td>Last Name :</td>
            <td><input name="lname" value="<?php if (isset($_POST["lname"])) {echo  v($_POST["lname"]);}?>"   required></input></td>
         </tr>
         <tr>
            <td>Email :</td>
            <td><input id="email" name="email" value="<?php if (isset($_POST["email"])) {echo  v($_POST["email"]);} else {echo "xo@xo.xo";}?>"  required></input></td>
         </tr>
         <tr>
            <td>Password :</td>
            <td><input name="pw" type="password" value="e" required></input></td>
         </tr>
         <tr>
            <td>Retype password :</td>
            <td><input name="pw" type="password" value="e" required></input></td>
         </tr>
      </table>
      <input type="submit" value="Register" ></input>
   </form>
   </body>
</html>

Yes. 是。 Although iterating over the fields gives you a little less clarity and makes it more messy when you want to determine how to validate said field (for example, to know if whether the value should be a name or a number or whatever), but you can do it this way: 虽然在字段上进行迭代会使您的清晰度降低一些,并且当您想要确定如何验证所述字段时(例如,知道该值是名称还是数字或其他内容)会使其更加混乱,但您可以这样做:

In your PHP script you could have something like: PHP脚本中,您可以使用以下内容:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Determines if the form was sent through the POST method

    foreach ($_POST as $fieldName => $formValue) {
        // Validate $formValue
    }
}

What I think you want to ask is if form data can be manipulated without knowing the form's variable names. 我想你想问的是,如果不知道表单的变量名称就可以操作表单数据。 I say this because you want to have a general purpose function where the code can be reused for any form. 我这样说是因为你想要一个通用的功能,代码可以重用于任何形式。 Since this may be any form that currently exists or a form you will create in the future you will not know the name of the variables in the form. 由于这可能是当前存在的任何形式或您将来创建的表单,因此您将不知道表单中变量的名称。

This code captures the form's input. 此代码捕获表单的输入。 All you have to do now is create a function that does whatever to the $name and $item values as they are looped through. 你现在要做的就是创建一个函数,它可以对$ name和$ item值进行循环处理。

<?php
foreach($_POST as $name => $item) {
  print "name::$name item::$item<br>";
} 
?>
<html><title>test</title>
<form method="post">
field one:  <input type="text" name="one">
<br>
field two:  <input type="text" name="two">
<input type="submit" value="go!">
</form>
</html>

Of course, it is possible to have the page in which the original form resides as the recipient of the form dialog. 当然,可以将原始表单所在的页面作为表单对话框的接收者。 Through the session variables, but mainly through the contents of the button variables you can determine which state your form is currently in (after having clicked a submit button you will get a $_REQUEST array element with the name of the button holding the value of the button). 通过会话变量,但主要通过按钮变量的内容,您可以确定您的表单当前处于哪种状态(在单击提交按钮后,您将获得一个$ _REQUEST数组元素,其中包含保存值的按钮的名称按钮)。

Take a look at the answer here . 看看这里答案

This is actually a canonical question for receiving form data in PHP. 这实际上是在PHP中接收表单数据的规范问题。 There are lots of ways to do it. 有很多方法可以做到这一点。

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

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