我的onclick第二次没有工作

时间:2016-01-27 17:51:38

标签: javascript jquery

每当用户点击注册或注册时,我都试图更改表单中的属性。因此表单会相应更改,但是一旦用户单击注册,他们就无法再单击登录。

 <?php
 session_start();



    if(isset($_POST['submit'])) {
        include_once("php/config/database.php"); 
        $Email = strip_tags($_POST['Email']);
        $dbPassword = strip_tags($_POST['Password']);

        $Email = stripslashes($Email);
        $dbPassword = stripslashes($dbPassword);

        $Email = mysqli_real_escape_string($conn, $Email);
        $dbPassword = mysqli_real_escape_string($conn, $dbPassword);



        $sql = ("SELECT * FROM 'Users' WHERE Email='$Email' AND Password='$dbPassword' LIMIT 1");
        $query = mysqli_query($conn, $sql);
        $row = mysqli_fetch_array($query);
        $UserID = $row['UserID'];
        $dbpass = $row['Password'];

        if($dbPassword == $dbpass) {
            $_SESSION['Email'] = $Email;
            $_SESSION['UserID'] = $UserID;
            header("Location: account.php");
        } 
        else 
        {
            echo "You didn't enter the correct details!";
        }

    } 


?>


<!DOCTYPE html>

<html >
  <head>
    <meta charset="UTF-8">
    <title>background</title>




        <link rel="stylesheet" href="css/style.css">
 <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

     <script src="js/index.js"></script>




  </head>

  <body>



     <form action="index.php" method="POST" id="search-theme-form">
  <div class="box">


    <h1 id="logintoregister">
      Login
    </h1>
    <div class="group show">
      <input class="inputMaterial" type="text" name="FirstName" />
      <label>First Name</label>
    </div>
    <div class="group show">
      <input class="inputMaterial" type="text" name="Surname" />
      <label>Surname</label>
    </div>
    <div class="group">
      <input class="inputMaterial" type="email" name="Email" required/>
      <label>Email</label>
    </div>
    <div class="group">
      <input class="inputMaterial" type="password" id="password"
      name="Password" required/> <label>Password</label>
    </div>
    <div class="group show">
      <input class="inputMaterial" type="password" id=
      "confirm_passwor" /> <label>Confirm Password</label>
    </div>


    <button id="buttonlogintoregister" type="submit" name=
    "submit">Login</button>




    <p id="plogintoregister"> By registering, You accept all terms and conditons</p>
    <p id="textchange" onclick="register()">Sign Up</p>


</div>
</form>



      <!-- Related demos -->




 <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

     <script src="js/index.js"></script>




</body>
</html>
  

JS档案

function register(){
var cont = 0;   

     cont++;

        if(cont==1){
            $('.box').animate({height:'600px'}, 550);
            $('.show').css('display','block');
            $('#logintoregister').text('Register');
            $('#buttonlogintoregister').text('Register');
            $('#plogintoregister').text("Sei gia' registrato?");
            $('#textchange').text('Login');
            $('#search-theme-form').removeAttr("action").attr("action", "signup_destination.php");



        }
        else
        {

            $('.show').css('display','none');
            $('.box').animate({height:'365px'}, 550);
            $('#logintoregister').text('Login');
            $('#buttonlogintoregister').text('Login');
            $('#plogintoregister').text("Non sei iscritto?");
            $('#textchange').text('Register');
            $('#search-theme-form').removeAttr("action").attr("action", "index.php");


            cont = 0;


        }
}

1 个答案:

答案 0 :(得分:2)

每次运行函数时都会继续重置cont var,

var cont = 0;  

因此,您将始终输入if的第一部分。

从函数中取出var count:

var cont = 0;
function register(){
..
..
.
}

这样,每次函数运行到0时都不会重置,其余的都应该有效。