什么是在PHP中验证模态的最简单方法

时间:2017-10-28 22:55:55

标签: php twitter-bootstrap validation modal-dialog

PHP / MySQL新手在这里。我正在尝试基于Bootstrap模式创建一个简单的注册表单。所以我在数据库中插入数据没有任何问题......我正在尝试在添加新数据时制定验证规则。也许我搞砸了用户类的东西。

//userClass.php
<?php
include('dblogin.php');
class User
{
   private $conn;
   public function __construct() 
    {
        $database = new Database();
        $db = $database->dbConnect();
        $this->conn = $db;
    }

    public function email_exists($Email) 
    {
        try
        { 
            $sql = "SELECT Email FROM users WHERE Email = '$Email'";

            $result = $this->conn->query($sql);

            if($result->num_rows == 1 ) {
                return true;
            } else {
                return false;
            }
            $this->conn->close();
        }catch(Exeption $e)
        {
            echo $e->getMessage();
        }
    }

    public function register($fName, $lName, $Email, $Pass) 
    {    
      try
       {
        $sql = "INSERT INTO users (Name, LastName, Email, User_pass)
            VALUES ('$fName', '$lName', '$Email', '$Pass')";

        $result = $this->conn->query($sql);
        $this->conn->close();
       }catch(Exeption $e) 
       {
           echo $e->getMessage();
       }
    }

这是register.php

 <div class="modal-dialog modal-lg" id="loginModal-content"> 
            ...
    </div>

<?php
    echo "<script>$('.alert').hide();</script>";

    $fName = $lName = $email = $pass = "";
    $USER = new User();

     if(isset($_POST['register']))
     {
        if(isset($_POST['fName']))
            $fName = $_POST['fName'];
        if(isset($_POST['lName']))
            $lName = $_POST['lName'];
        if(isset($_POST['email']))
            $email = $_POST['email'];
        if(isset($_POST['pass']))
            $pass = $_POST['pass'];

         if($fName == '' || $lName == '')
         {
             echo "<script type='text/javascript'>
                        $('#registerModal').modal('show');   
                        $('.alert').fadeIn();
                    </script>";
         }

        else if(!$USER->email_exists($email))
        {
          echo "<script type='text/javascript'>
                        $('#registerModal').modal('show');   
                        $('.alert').fadeIn();
                    </script>";
        }


        $USER->register($fName, $lName, $email, $pass);    
     }
?>

当我尝试插入已经在数据库中的电子邮件时出现问题。警报出现但我模态隐藏所以在重新打开模态后看到它。

0 个答案:

没有答案