无法找出我的验证错误


can't figure out my validation error

我还有一个问题要问你们。 所以我是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

问题是,由于我的表单名称,我不太确定如何处理它:( 与往常一样,非常感谢任何帮助:)

这是我的注册.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>

$_POST['newUser']['email']

干杯

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

但仅适用于 PHP。在您的表格中,您可以不带引号。因为那是HTML而不是PHP。(我想!

我认为你不能直接通过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"]