-1

大家好,我是编码新手,喜欢它的每一分钟:)
所以下面的代码在我的 registration.php 文件中。我想这样做,以便当用户填写所有内容之前,它会在按下注册按钮后将它们定向到我的 invoice.php 文件。如果他们缺少某些要求,请返回注册表并(希望在我弄清楚这一点之后,放置一些粘性表格,这样他们就不必输入任何经过验证的文本)另外,收到错误“通知:未定义索引:在我的 PHP 代码顶部的验证中,在第 9 行的 C:\xampp\htdocs\assignment_2\registration.php中提交,不太确定我应该放什么。:( 一如既往,非常感谢任何帮助!

    <html>
    <h4>
        <center>
            New User Registration
        </center>
    </h4>
    <body>
<?php
if($_POST["submit"]=="login")
    {
        if(preg_match("/^[0-9a-zA-Z_]{5,}$/", $_POST["user"]) === 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["pass"]) === 0)
          $errPass = '<p class="errText">Password must be at least 8 characters and must contain at least one lower case letter, one upper case letter and one digit</p>';
    // Email mask
        if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["email"]) === 0)
          $errEmail = '<p class="errText">Email must comply with this mask: chars(.chars)@chars(.chars).chars(2-4)</p>';


    }    




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

2 回答 2

1

The problem here is one that many programmers that are new to PHP run into. See, $_POST is an array that includes all parameters that have been submitted with the POST request the script was requested with. But what if there were none, or the script was requested using GET? You got it: it's empty.

In your case, submit doesn't seem to be present in the POST data. In that case you most likely want to just display the form, so the user can enter the data and submit the form (which will set that value). SO you have to check FIRST if "submit" is there.

Second: Your submit button is called "register", and it's value (which is not really necessary, but alright) is "Register". So if the form is submitted, the data that is sent is register=Register&<other fields>. Therefore you have to check for the presence of "register", not "submit"

if (isset($_POST['register']) && $_POST['register']) {
    // evaluate
}
于 2013-11-06T04:36:27.407 回答
0

你没有检查任何 var 设置所以你可以看到更多的 php 错误

我换了更多的支票,你可以试试

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

      $errEmail    = "";
      $errUser     = "";
      $errPass     = "";

      //not too sure what this if statement suppose to be.
      if(isset($_POST["register"])){//1. no '{' 2. the post is not ckeck 'type', is 'name'

        // Email mask
        if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["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["user"]) === 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["pass"]) === 0)
          $errPass = '<p class="errText">Password must be at least 8 characters and must contain at least one lower case letter, one upper case letter and one digit</p>';
      }    
  ?>
        <form method="post">
            <center>
                <table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
                    <tr>
                        <td>Username</td>
                        <td>:</td>
                        <td ><input name="user" type="text" size="16" value="<?php echo (isset($_POST["user"]))?$_POST["user"]:'';/*3.no check the var is set?*/ ?>">
          <?php  if(isset($errUser) and $errUser !='') echo $errUser; ?>
                        </td ><!--4. no '</td>'-->
                    </tr>
                     <tr>
                        <td>Password</td>
                        <td>:</td>
                        <td><input name="pass" type="password" size="16" value="<?php echo (isset($_POST["pass"]))?$_POST["pass"]:''; ?>">
          <?php  if(isset($errPass) and $errPass !='') echo $errPass; ?>
                        </td >
                    </tr>
                                         <tr>
                        <td>Email</td>
                        <td>:</td>
                        <td><input name="email" type="text" size="50" value="<?php echo (isset($_POST["email"]))?$_POST["email"]:''; ?>">
          <?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>
于 2013-11-06T04:41:25.677 回答