0

我有一些我不太明白的错误。以下错误是:

Notice: Undefined index: newUser in C:\xampp\htdocs\assignment_2\registration.php on line 24

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

Notice: Undefined index: newUser in C:\xampp\htdocs\assignment_2\registration.php on line 67

Notice: Undefined index: newUser in C:\xampp\htdocs\assignment_2\registration.php on line 71

这很奇怪,因为我昨天发誓不存在错误,我还在 netbeans 中进行了历史比较,结果还是一样:(

这是我的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']['ID'];


// 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 "<p><center>Please fill in all text boxes</center></p>";
        }
        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"])){

        // User must be digits and letters
        if(preg_match("/^[0-9a-zA-Z]{5,}$/", $_POST['newUser']['ID']) === 0)
          $errUser = '<span class="error">Username must be more than 5 characters and contain letters and numbers.</span>';

        // Password must be strong
        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>';

        //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 = '<span class="error">example: chars(.chars)@chars(.chars).chars(2-4)</span>';
      }  
  ?>

        <form action = "invoice.php" method= 'get'> 
            <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'><br>
            </center>    
        </form>
    </body>
</html>

任何帮助将不胜感激。谢谢!

4

3 回答 3

0

未定义的索引意味着newUser未在$_POST超全局中发送。它仍然是你的形式吗?

示例表单指向invoice.php但您说这是registration.php

这个示例表单通过发送method=get,这将是$_GET

于 2013-11-10T04:51:25.897 回答
0

你改变$_POST变量了吗?这个片段是一个大线索:

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

那是检查是否$_POST['register']存在,但从$_POST['newUser']. 所以也许是这样的:

if (array_key_exists('register', $_POST))

应该改成这样:

if (array_key_exists('newUser', $_POST))

或者像这样发生的变量设置:

$the_user = $_POST['newUser']['ID'];

应该改成这样:

$the_user = $_POST['register']['ID'];

但它似乎registernewUser混杂在您$_POST和您的一般代码逻辑中。

于 2013-11-10T04:54:20.307 回答
0

因为这是一个赋值 - 在你的 include 语句之后插入 var_dump($_POST);

<?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";
var_dump( $_POST );
// Define the datafile to hold the $users array
 $datafile = "users.dat";

然后根据需要调整您的代码。

于 2013-11-10T04:55:17.483 回答