php
  • html
  • 2013-03-28 97 views 1 likes 
    1

    編輯
    感謝,韋斯CI現在有這樣的AJAX代碼:允許登錄表單處理同一個頁面上的PHP腳本而無需刷新頁面

    <script type="text/javascript"> 
    $(document).ready(function(){ 
        $("form[name^='login']").submit(function(event) { 
         event.preventDefault(); 
    
         var dataToSend = 'username='+$("input[name^='username']").val()+'&password='+$("input[name^='password']").val(); 
    
         $.ajax({ 
          type: "POST", 
          url: "index.php", 
          data: dataToSend, 
          success: function(response){ 
           if(response == "REDIRECT") 
           { 
            window.location = "business_profiles/myReviews.php"; 
           else 
           { 
            $("#my_error_div").html(response); 
           } 
          } 
         }); 
        }); 
    }); 
    </script> 
    

    但現在,而不是顯示錯誤消息時例如鍵入你的密碼框。無論表單輸入是什麼,我收到的所有信息都是一個提示框,顯示「成功」。

    好吧,我試過並試圖找出我自己的。以及聯繫到其他編碼人員,當然搜索stackoverflow的答案。沒有一個看起來與我的情況相符。

    我有一個簡單的登錄表單,像這樣:

    <form name="login" action="index.php" method="post"> 
    
         <ul> 
    
          <li> 
           <input type="text" name="username" autocomplete="off" placeholder="Username" autofocus="true"></li> 
    
          <li> 
           <input type="password" name="password" autocomplete="off" placeholder="Password"></li> 
          <li> 
    
           <input type="submit" name="submit" value="Login"> <a href="register.php" id="button">Register</a> <a href="forgot.php" id="button">Forgot Password</a> <a href = "javascript:void(0)" onclick = "document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'" id="button">Close</a> 
    
          </li> 
         </ul> 
        </form> 
    

    這種形式提交到它是在同一頁上。 (PHP腳本是正確的登錄表單之上。),這是如下:

    <?php 
    
    //If the user has submitted the form 
    if($_POST['username']){ 
        //protect the posted value then store them to variables 
        $username = protect($_POST['username']); 
        $password = protect($_POST['password']); 
    
        //Check if the username or password boxes were not filled in 
        if(!$username || !$password){ 
         //if not display an error message 
         echo "<center>You need to fill in a <b>Username</b> and a <b>Password</b>!</center>"; 
        }else{ 
         //if the were continue checking 
    
         //select all rows from the table where the username matches the one entered by the user 
         $res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."'"); 
         $num = mysql_num_rows($res); 
    
         //check if there was not a match 
         if($num == 0){ 
          //if not display an error message 
          echo "<center>The <b>Username</b> you supplied does not exist!</center>"; 
         }else{ 
          //if there was a match continue checking 
    
          //select all rows where the username and password match the ones submitted by the user 
          $res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."' AND `password` = '".$password."'"); 
          $num = mysql_num_rows($res); 
    
          //check if there was not a match 
          if($num == 0){ 
           //if not display error message 
           echo "<center>The <b>Password</b> you supplied does not match the one for that username!</center>"; 
          }else{ 
           //if there was continue checking 
    
           //split all fields fom the correct row into an associative array 
           $row = mysql_fetch_assoc($res); 
    
           //check to see if the user has not activated their account yet 
           if($row['active'] != 1){ 
            //if not display error message 
            echo "<center>You have not yet <b>Activated</b> your account!</center>"; 
           }else{ 
            //if they have log them in 
    
            //set the login session storing there id - we use this to see if they are logged in or not 
            $_SESSION['uid'] = $row['id']; 
            //show message 
            echo "<center>You have successfully logged in!</center>"; 
    
            //update the online field to 50 seconds into the future 
            $time = date('U')+50; 
            mysql_query("UPDATE `users` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'"); 
    
            //redirect them to the usersonline page 
            echo 'REDIRECT'; 
    
            exit; 
           } 
          } 
         } 
        } 
    } 
    
    ?> 
    

    我需要的形式處理腳本而無需刷新頁面。我的登錄表單位於燈箱內,因此如果在頁面刷新燈箱時隱藏了無效密碼等錯誤,則必須再次單擊登錄以查明您做錯了什麼。我只是希望表單在不刷新頁面的情況下處理php腳本,因此燈箱永遠不會隱藏,直到用戶成功登錄。然後用戶被重定向到他們的配置文件。

    +2

    你的意思是像AJAX顯示頁面? – intelis 2013-03-28 04:00:47

    +1

    對於這個場景使用'AJAX'。 – 2013-03-28 04:01:55

    +0

    因此,我將不得不將腳本的所有編碼更改爲AJAX? – user2109152 2013-03-28 04:03:26

    回答

    1

    使用jQuery,你可以使用下面的AJAX功能後,從登錄表單到同一頁中的數據:

    <script type="text/javascript"> 
    $(document).ready(function(){ 
        $("form[name^='login']").submit(function(event) { 
         event.preventDefault(); 
    
         var dataToSend = 'username='+$("input[name^='username']").val()+'&password='+$("input[name^='password']").val(); 
    
         $.ajax({ 
          type: "POST", 
          url: "index.php", 
          data: dataToSend, 
          success: function(response){ 
           if(response == "REDIRECT") 
           { 
            window.location = "business_profiles/myReviews.php"; 
           } 
           else 
           { 
            alert("Error: "+response); 
            $("#my_error_div").html(response); 
           } 
          } 
         }); 
        }); 
    }); 
    </script> 
    

    此外,改變你的PHP來:

    <?php 
    
    //If the user has submitted the form 
    if(isset($_REQUEST['username'])){ 
        //protect the posted value then store them to variables 
        $username = protect($_POST['username']); 
        $password = protect($_POST['password']); 
    
        //Check if the username or password boxes were not filled in 
        if(!$username || !$password){ 
         //if not display an error message 
         echo "<center>You need to fill in a <b>Username</b> and a <b>Password</b>!</center>"; 
        }else{ 
         //if the were continue checking 
    
         //select all rows from the table where the username matches the one entered by the user 
         $res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."'"); 
         $num = mysql_num_rows($res); 
    
         //check if there was not a match 
         if($num == 0){ 
          //if not display an error message 
          echo "<center>The <b>Username</b> you supplied does not exist!</center>"; 
         }else{ 
          //if there was a match continue checking 
    
          //select all rows where the username and password match the ones submitted by the user 
          $res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."' AND `password` = '".$password."'"); 
          $num = mysql_num_rows($res); 
    
          //check if there was not a match 
          if($num == 0){ 
           //if not display error message 
           echo "<center>The <b>Password</b> you supplied does not match the one for that username!</center>"; 
          }else{ 
           //if there was continue checking 
    
           //split all fields fom the correct row into an associative array 
           $row = mysql_fetch_assoc($res); 
    
           //check to see if the user has not activated their account yet 
           if($row['active'] != 1){ 
            //if not display error message 
            echo "<center>You have not yet <b>Activated</b> your account!</center>"; 
           }else{ 
            //if they have log them in 
    
            //set the login session storing there id - we use this to see if they are logged in or not 
            $_SESSION['uid'] = $row['id']; 
    
    
            //update the online field to 50 seconds into the future 
            $time = date('U')+50; 
            mysql_query("UPDATE `users` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'"); 
    
            //redirect them to the usersonline page 
            echo 'REDIRECT'; 
           } 
          } 
         } 
        } 
    
        exit; 
    } 
    
    ?> 
    
    +0

    謝謝你的回答!我將上面的代碼添加到我的表單中,並且它仍然刷新...請看看我正在處理的內容。 (點擊登錄,然後輸入用戶名和密碼,然後點擊登錄。)http://www.green-panda.com/review-pratt/index.php – user2109152 2013-03-28 04:41:26

    +0

    對不起!我稍微修改了一下代碼,它應該解決這個問題。 – 2013-03-28 04:49:28

    +0

    我編輯了我的問題。請看一下。 – user2109152 2013-03-28 04:51:55

    0

    第一步:添加一個div給你的登錄表單。

    第二步:驗證用戶控件,然後在Ajax調用

    第三步加載頁面:在阿賈克斯成功事件追加你想使用.html()

    相關問題