如何回应"用户名/电子邮件不应为空!"在田野里?

时间:2018-03-25 10:13:58

标签: php

好的,现在当用户没有正确填写字段时,他被重定向到这个enter image description here我想要的是使它更加用户友好,因此用户可以在字段中看到他们的错误或至少在同一页面中,没有被重定向到这个白页。

到目前为止,这是我的代码:

Conn.php

<?php


session_start();
$username = $_POST['username'];
$password =$_POST['password'];
$email = $_POST ['email'];
$phone = $_POST['phone'];



if(!empty ($username) && !empty($email)){
    if(!empty ($password)){



        $host = "host";
        $dbusername = "user";
        $dbpassword = "********";
        $dbname = "dbname";

        $conn = new mysqli ($host,$dbusername,$dbpassword,$dbname);

        if(mysqli_connect_error()){
            die('Connect Error ('. mysqli_connect_errno().')' . mysqli_connect_error());
        }



else{

    $username= mysqli_real_escape_string($conn,$_POST["username"]);
    $email= mysqli_real_escape_string($conn,$_POST["email"]);
    $phone= mysqli_real_escape_string($conn,$_POST["phone"]);
    $password= mysqli_real_escape_string($conn,$_POST["password"]);
    $password = password_hash($password,PASSWORD_DEFAULT);  

    $sql = "INSERT INTO user(username,password,email,phone) values('$username','$password','$email','$phone')";
    if($conn->query($sql)){ 
    $_SESSION['username'] = $username;
    $_SESSION['success'] = "You are now logged in";
    header('location: index.php');
    }
    else{
        echo "error: ". $sql."<br>".$conn->error;
    }
    $conn->close();
}       



        }
    else {
        echo "Password should not be empty";
        //echo "<script>alert('Password should not be empty!');</script>";
        die();
    }
}

else{
    echo "Username/Email should not be empty";
    die();  
}



?>

SignUp.php

<?php
include_once 'header.php';

?>

<h1 class="register"> Rigester </h1>
<div class="help" >

<form  method="POST" action="conn.php">



      <div class="help">
      <input placeholder="Username" type="text" name="username" >
      </div>
       <div class="help">
      <input placeholder="Password" type="password" name="password" >
      </div>
       <div class="help">
      <input placeholder="Email" type="text" name="email" >
      </div>
       <div class="help">
      <input placeholder="Phone" type="text" name="phone" >    
      </div>

      <input  class="helpbtn" type="submit" name="submit" >

</form>   
</div>
<?php
include_once 'footer.php';

?>

1 个答案:

答案 0 :(得分:0)

你可以像这样使用它 在conn.php中:

// form error
if(condiftion){
    header("location:www.example.com/SignUp.php?msg=1");
}

in signup.php

//you can print diffrent message By judging value of $_GET[''msg'] with switch
<?php echo isset($_GET["msg"]) "your message" : "";?>