我如何在PHP中解决未定义的变量

时间:2018-03-28 19:51:17

标签: php mysql forms mysqli phpmyadmin

我正在开发一个注册页面,它将在我编写表单的同一页面上检查输入的验证。问题说未定义的变量,我不能总结并解决这个问题,这些变量的问题是什么? 我使用全局变量来访问函数外部的变量,另外如果我在另一个函数中调用一个函数会有问题,或者它也会给我错误。

<!DOCTYPE html>
<html>
<head>
<link>
<style media="screen">
</style>
<title> Sign up </title>
</head>
<body>
<h2><center> Sign up </center></h2>

<?php
  // setting inputs into variables
  if($_SERVER["REQUEST_METHOD"] == POST){
    $name = $_POST['Fullname'];
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $passordRecheck = $_POST['RePassword'];
    $gender = $_POST['gender'];
    $phoneNumber = $_POST['phoneNumber'];
    $creditNumber = $_POST['creditCardNumber'];
    // set new varibles for errors
    $nameErr = "";
    $usernameErr ="";
    $emailErr="" ;
    $passwordErr="";$genderErr="";$phoneNumberErr="";$creditNumberErr ="";

    function inputsValidation(){

        //check for validation of inputs
        if(empty($GLOBALS['name'])){
          $GLOBALS['nameErr'] = "Name must not be empty";
        } else {
          $GLOBALS['name'] = test_input($GLOBALS['name']);
          if(!preg_match("/^[a-zA-Z ]*$/",$GLOBALS['name'])){
            $GLOBALS['nameErr'] = "Only alpahbets are allowed";
          }
        } // end of name validation
        if(empty($GLOBALS['username'])){
          $GLOBALS['usernameErr'] = "Username must not be empty";
        } else {
          $GLOBALS['username'] = test_input($GLOBALS['username']);
          if(!preg_match("/^[A-Za-z][A-Za-z0-9]{5,31}$/",$GLOBALS['username'])){
            $GLOBALS['usernameErr']= "Only alphabets and numbers are allowed, must start with letter";
          }
    } // end of username validation

    if(empty($GLOBALS['email'])){
      $GLOBALS['$emailErr'] = "Email must not be empty";
    } else {
      $GLOBALS['email'] = test_input($GLOBALS['email']);
      if(!filter_var($GLOBALS['email'], FILTER_VALIDATE_EMAIL)){
        $GLOBALS['emailErr'] = "Email didn't entered in a correct form";
      }
    } // end of email validation
    if(empty($GLOBALS['password'])){
      $GLOBALS['passwordErr'] = "Password must not be empty";
    } else {
      if(!preg_match("/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{8,12}$/",$GLOBALS['password'])){
        $GLOBALS['passwordErr'] = "One capital letter and at least one number";
      }
    } // end of password validation
    if(empty($GLOBALS['gender'])){
      $GLOBALS['genderErr'] = "gender must not be empty";
    } // end of gender validation
    if(empty($GLOBALS['phoneNumber'])){
      $GLOBALS['phoneNumberErr'] = "Phone number must not be empty";
    } else {
      $GLOBALS['phoneNumber'] = test_input($GLOBALS['phoneNumber']);
      if(!preg_match("/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/",$GLOBALS['phoneNumber'])){
        $GLOBALS['phoneNumberErr'] = "Only + and numbers";
      }
    } // end of phone number validation
    // if(empty($creditNumber)){
    //   $creditNumberErr = "credit Card number must not be empty";
    // } else {
    //   $creditNumber = test_input($creditNumber);
    //   if(!preg_match("",$creditNumber)){
    //     $creditNumberErr = "only numbers are allowed";
    //   }
    // } // end of credit card number validation
  }

  function test_input($data) { // function to remove whitespaces,slashes,...
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
  } //end of function
}
   ?>

<form method="POST" action="signupProcess.php" onsubmit="inputsValidation()">
    <label><b>Name: </b></label>
    <input type="text" placeholder="Enter name" name="Fullname">
    <span><?php echo $nameErr ?></span><br><br>

    <label><b>Username: </b></label>
    <input type="text" placeholder="Enter Username" name="username">
    <span><?php echo $usernameErr ?></span><br><br>

    <label><b>Email: </b></label>
    <input type="text" placeholder="Enter Email" name="email">
    <span><?php echo $emailErr ?></span><br><br>

    <label><b>Password: </b></label>
    <input type="password" placeholder="Enter Password" name="password">
    <span><?php echo $passwordErr ?></span><br><br>

    <label><b>Re-Type Password: </b></label>
    <input type="password" placeholder="RePassword" name="RePassword">
    <span><?php echo $passwordErr ?></span><br><br>

    <label><b>gender</b></label>
    <input class="t" type="text" placeholder="Re-TypePassword" name="gender">
    <span><?php echo $genderErr ?></span><br><br>

    <label><b>phone Number: </b></label>
    <input type="number" placeholder="phone number" name="phoneNumber">
    <span><?php echo $phoneNumberErr ?></span><br><br>

    <!-- <label><b>credit card number </b></label>
    <input type="text" placeholder="Re-TypePassword" name="creditCardNumber">
    <span></span><br><br> -->

    <input type="submit" name="submit" value="submit">
</form>
</body>
</html>
`

结果如下:

undefined variables

0 个答案:

没有答案