尝试将数据插入mySql数据库时出错

时间:2017-03-29 22:24:54

标签: javascript html ajax

在这些日子里,我尝试用php和mySql编写一种注册登录系统,不知怎的,我设法做到了;现在我想把所有的东西放在一个带弹出窗口的新页面中:你按下按钮"注册"或者"登录"并在屏幕上显示一个包含所有内容和东西的窗口,这就是我所做的:

<!DOCTYPE html>
<html>
<head>
<title>FindMyChamp</title>
</head>
<body>
<style type="text/css">
    .popUpBox{
        display: none;
        position: fixed;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        background-color: rgba(0,0,0,0.4);
    }
    .popUpBoxBody{
        margin: 15% auto;
        padding: 15px;
        background-color: #fefefe;
        width: 30%;
    }
    .closeBtn{
        float: right;
        font-size: 28px;
    }
    .container{
        width: 300px;
        clear: both;
        display: block;
      margin-left: auto;
      margin-right: auto;
      text-align: center;
    }
    .container input{
        width: 100%;
        clear: both;
    }
    #send{
        width: 50%;
    }
</style>

<h1>Test</h1>
<p id="popUpTrigger">Register</p> 
<p>Login</p>
<div id="divPopUp" class="popUpBox">
    <div class="popUpBoxBody">
        <span id="popUpCloser" class="closeBtn">&times;</span>
        <div class="container">
            <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                Username: <input type="text" name="username">
                Password: <input type="password" name="password">
                Repeat Password: <input type="password" name="passwordCheck"><br><br>           
                <input value="Send" id="send" type="submit" name="sub">
            </form>     
            <?php include('registration.php') ?>
        </div>
    </div>
</div>
<script type="text/javascript">
    var btnOpen = document.getElementById("popUpTrigger");
    var popUp = document.getElementById("divPopUp");
    var btnClose = document.getElementById("popUpCloser")
    btnOpen.onclick = function(){
        popUp.style.display = "block";
    }
    btnClose.onclick = function(){
        popUp.style.display ="none";
    }
</script>

register.php:

<?php
function test_input($data){
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
if(isset($_POST["sub"])){
    $username = $password = $passwordCheck = "";
    $flag = true;
    $con = mysql_connect('localhost','root','Testpass');
    if($_SERVER["REQUEST_METHOD"] == "POST"){
        //Controllo se i campi sono vuoti
        if(empty($_POST["username"])){
            echo "The field 'Username' is required<br>";
        } else{
            $username = test_input($_POST["username"]);             
        }
        if(empty($_POST["password"])){
            echo "The field 'Password' is required<br>";
            $flag = false;
        } else{
            $password = test_input($_POST["password"]);             
        }
        if(empty($_POST["passwordCheck"])){
            echo "The field 'Repeat Password' is required<br>";
            $flag = false;
        } else{
            $passwordCheck = test_input($_POST["passwordCheck"]);               
        }   
    }
    if($password == $passwordCheck && $password != ""){
        mysql_select_db('tutorials');
        $checkUsernameDuplicate = mysql_query("SELECT * FROM registration WHERE username = '$username'");
        if(mysql_num_rows($checkUsernameDuplicate) <= 0){
            $cost = 10;             
            $salt = strtr(base64_encode(mcrypt_create_iv(16,MCRYPT_DEV_URANDOM)), '+', '.');
            $salt = sprintf("$2a$%02d$", $cost). $salt;
            $hash = crypt($password, $salt);
            $sql = "INSERT INTO registration(username,password) VALUES('$username','$hash')";
            $retvalue = mysql_query($sql,$con);
            if(!$retvalue){
                echo "Something went wrong";
            } else{
                echo "Dati inseriti";
                //header("Location: http://localhost/database/login.php");              
                exit();     
            }

        } else{
                echo "Username aldready taken";
        }
        mysql_close($con);      
    } 
    elseif($flag){
        echo "<p style='color: red;'>The two password must match</p>";
    }
}
?>

一切正常,但是,当我按下按钮时,发送&#39;窗口消失,所有数据都发送到数据库,没关系,但我希望窗口保持不变,直到用户决定关闭它。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

使用ajax,您可以在register.php中执行代码而无需刷新页面 http://api.jquery.com/jquery.ajax/