PHP中的post变量中的数据验证索引未定义(registration.php)

时间:2013-11-10 04:48:26

标签: php validation

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

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>

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

3 个答案:

答案 0 :(得分:0)

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

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

此示例表单通过method=get发送,即$_GET

答案 1 :(得分: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和您的通用代码逻辑。

答案 2 :(得分: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";

然后根据需要调整代码。