2016-03-07 118 views
0

这是我第一次验证,我有已经花了无数的时间在这个最困难的时候。我有一个需要验证的注册表单,我已经为此尝试了2个脚本。效果最好的脚本可以看到下面:但是每一次我尝试呼应的错误消息在我的文本字段,以显示我收到以下错误信息:PHP的表单验证错误


注意:未定义的变量:c_email在/应用上线/MAMP/htdocs/PhpProject2/Reg_1.php

说明:未定义变量:c_emailErr在/Applications/MAMP/htdocs/PhpProject2/Reg_1.php线163

说明:未定义变量:/ Applicat中的c_pass1Err上线离子/ MAMP/htdocs中/ PhpProject2/Reg_1.php 169

C_emailErr和c_pass1Err都定义。

任何帮助,将不胜感激。

HTML

  <section class="container"> 
     <form id="myform " class="Form" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" accept-charset="utf-8"> 

      <!--<div id="first">--> 
      <input type="email" id="email" name="email" placeholder="Email Address" value="<?= $c_email ?>" required > 
      <br> 
      <span class="error"><?php echo $c_emailErr; ?></span> 
      <br> 

      <figure> 
       <input class ="login-field" type="password" id="pass1" name="pass1" value="<?= $c_pass1 ?>" placeholder="Password" maxlength="30" required> 
       <br> 
       <span class="error"><?php echo $c_pass1Err; ?></span> 
       <br> 
       <input class ="login-field" type="password" id="pass2" name="pass2" value="" placeholder=" Confirm password" maxlength="30" required><!--<span class="error"><?php //echo $c_pass2Err;    ?></span>--> 
       <div id="messages"></div> 
      </figure> 
      <p class="remember_me"> 
      </p> 
      <input type="submit" name="submit" value="Register" id="submit_button" class="btn btn-default"> 
      <br> 
     </form> 

     <?php 

     ?> 

    </form> 
</section> 

PHP

<?php 
       if (isset($_POST['submit'])) { 


        $c_email = $_POST['email']; 
        $c_pass1 = $_POST['pass1']; 
        $c_pass2 = $_POST['pass2']; 
        $c_emailErr = $c_pass1Err = $c_pass2Err = ""; 
        //Checking the email address 
        if (!filter_var($c_email, FILTER_VALIDATE_EMAIL) === false) { 
         echo ("<b id='email'> This is a valid email address </b>"); 
        } else { 
         echo ("<b id='email'> Email is not a valid email address</b>"); 
        } 
        if (strlen($c_pass1) <= '8') { 
         echo "<b>Your Password Must Contain At Least 8 Characters!</br>"; 
         //check passwords 
        } elseif ($c_pass1 == $c_pass2) { 
         $q = "INSERT INTO Cus_Register(Cus_Email,Cus_Password,Cus_confirm_password) VALUES (?,?,?)"; 
         $stmt = mysqli_prepare($dbc, $q); 
         //new 
         // $stmt = mysqli_prepare($dbc, $insert_c); 
         //debugging 
         //$stmt = mysqli_prepare($dbc, $insert_c) or die(mysqli_error($dbc)); 

         mysqli_stmt_bind_param($stmt, 'sss', $c_email, $c_pass1, $c_pass2); 


         if ($q) { 
          echo "<script> alert('registration sucessful')</script>"; 
         } 
        } else { 
         echo "<b>Oops! Your passwords do not </b>"; 
        } 
       } 

       ?> 
+0

[PHP: “注意:未定义变量” 和 “通知:未定义的索引”]的可能的复制(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice -undefined-index) – Qirel

+0

PHP和HTML代码是否包含在同一个文件中? OMG! – apokryfos

回答

2

您正在定义的变量,但要定义if块内他们..移动它们if块之外。

<?php 
$c_emailErr = $c_pass1Err = $c_pass2Err = ""; 

if (isset($_POST['submit'])) { 
    $c_email = $_POST['email']; 
    $c_pass1 = $_POST['pass1']; 
    $c_pass2 = $_POST['pass2']; 
+0

是它。你是活着的救星!非常感谢你 – jerneva

+0

没问题!旧的第二双眼睛的东西:) – mituw16