无法弄清楚我的验证错误

时间:2013-11-08 08:21:18

标签: php forms validation

我还有另外一个问题。所以我是PHP的新手,当我遇到这些错误时,我正在尝试验证我的代码:

Notice: Undefined index: newUser[email] in C:\xampp\htdocs\assignment_2\registration.php on line 63

Notice: Undefined index: newUser[ID] in C:\xampp\htdocs\assignment_2\registration.php on line 66

Notice: Undefined index: newUser[password] in C:\xampp\htdocs\assignment_2\registration.php on line 70

问题是,我不太确定如何对待它因为我的表格名称:(一如既往,非常感谢任何帮助:)

这是我的registration.php代码

<html>
    <h4>
        <center>
            New User Registration
        </center>
    </h4>
    <body>

        <?php
        /*
         * What this code does, is it takes the users registration information, and stores it in a file.
         * After that what I hope to do is to retrive the same file when the user is on the login page and if 
         * the login matches whats in the file, then proceed to the invoice page.
         */
include "functions.inc";

    // Define the datafile to hold the $users array
    $datafile = "users.dat";

    // See if the user has submitted something
    if (array_key_exists('register', $_POST))
    {
        // Get the new user info
            $the_user = $_POST['newUser'];


// Load the file of users and store it in $users
        $users = arrayfile_to_array($datafile);


        // Validate user name and password
        if (user_exists($the_user['ID'], $users))
        {
            echo "User already exists!";
        }
        else
        {
            // If valid, save to the file of users
            $users[] = $the_user;
            array_to_arrayfile($users, $datafile);
        }
    }
    else
    {
        if (!file_exists($datafile))  // Data file doesn't exist, so create it
        {
            $users = array();
            array_to_arrayfile($users, $datafile);
        }
    }
?>

        <?php
       // my defined error values 
      $errEmail    = "";
      $errUser     = "";
      $errPass     = "";


      if(isset($_POST["register"])){

        // Email validation
        if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["newUser[email]"]) === 0)
          $errEmail = '<p class="errText">Email must comply with this mask: chars(.chars)@chars(.chars).chars(2-4)</p>';
        // User must be digits and letters
        if(preg_match("/^[0-9a-zA-Z_]{5,}$/", $_POST["newUser[ID]"]) === 0)
          $errUser = '<p class="errText">User must be bigger that 5 chars and contain only digits, letters and underscore</p>';
        // Password must be strong
        //if(preg_match("/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $_POST["pass"]) === 0)
        if(preg_match("/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $_POST["newUser[password]"]) === 0)
          $errPass = '<span class="error">Password must be at least 8 characters and must contain at least one lower case letter, one upper case letter and one digit</span>';
      }  
  ?>

        <form action = "<?= $_SERVER['PHP_SELF'] ?>" method= 'POST'> 
            <center>
                <table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
                    <tr>
                        <td>Username</td>
                        <td>:</td>
                        <td ><input name="newUser[ID]" type="text" size="16" value="">
          <?php  if(isset($errUser) and $errUser !='') echo $errUser; ?>
                        </td >
                    </tr>
                     <tr>
                        <td>Password</td>
                        <td>:</td>
                        <td><input name="newUser[password]" type="password" size="16" value="">
          <?php  if(isset($errPass) and $errPass !='') echo $errPass; ?>
                        </td >
                    </tr>
                                         <tr>
                        <td>Email</td>
                        <td>:</td>
                        <td><input name="newUser[email]" type="text" size="50" value="">
          <?php  if(isset($errEmail) and $errEmail !='') echo $errEmail; ?>
                        </td >
                    </tr> 
                    <tr>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td> 
                    </tr>          
                </table>
                <input type='submit' name='register' value='Register'>
            </center>    
        </form>
    </body>
</html>

3 个答案:

答案 0 :(得分:0)

我认为你应该使用$_POST['newUser']['email']

但仅限于PHP。在您的表单中,您可以不带引号而留下它们。既然是HTML而不是PHP。 (我想!)

答案 1 :(得分:0)

$ _ POST [ 'NEWUSER'] [ '电子邮件']

干杯

答案 2 :(得分:-1)

我认为您不能直接通过HTML表单创建数组并将其作为POST-Variables提交。您必须重命名HTML表单中的变量。例如

<input name="newUser[password]" type="password" size="16" value="">

<input name="newUser_password" type="password" size="16" value="">

相应的PHP:

$_POST["newUser_password"]