2015-10-16 97 views
3

執行jquery表單驗證並將function btn_onclick() { window.location.href = "http://localhost/loginprivate.php";}index.php重定向到loginprivate.php我的webapps php腳本不會被執行。用戶變成槽功能和window.location.hrefindex.php重定向loginprivate.php你可以在這裏看到。第一次頁面加載後頁面變得再次加載<script src="loadagain.js"></script>這工作得很好。jQuery的表單驗證和PHP腳本在一個頁面

現在的問題是,如果我點擊提交按鈕的PHP代碼將不會執行,我可以看到,因爲沒有創建cookie,並且用戶被重定向到index.php

我的PHP代碼:

if(isset($_POST["submit"])) 
    { 
      $hostname='localhost'; 
      $username='root'; 
      $password=''; 

      unset($_POST['password']); 
      $salt = ''; 
      for ($i = 0; $i < 22; $i++) { 
        $salt .= substr('./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', mt_rand(0, 63), 1); 
      } 
      $_POST['password'] = crypt($_POST['password'],'$2a$10$'.$salt); 
      $new = 0; 
      try { 
        $dbh = new PDO("mysql:host=$hostname;dbname=search",$username,$password); 
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line 
        $sql = "INSERT INTO users (username, password) 
        VALUES ('".$_POST["username"]."','".$_POST["password"]."')"; 
        if ($dbh->query($sql)) { 
          echo "New Record Inserted Successfully"; 
        } 
        else{ 
          echo "Data not successfully Inserted."; 
        } 
        $new = $dbh->lastInsertId(); 
        $dbh = null; 
      } 
      catch(PDOException $e) 
      { 
        echo $e->getMessage(); 
      } 
      if ($new > 0) 
      { 

       $t = time() + 60 * 60 * 24 * 1000; 
       setcookie("username", $_POST['username'], $t); 
       setcookie("userid", $new , $t); 
      } 
     else 
      { 

      } 
    } 

我的html代碼:

<head> 
     <meta charset="UTF-8" /> 
     <title> 
     HTML Document Structure 
     </title>  
     <link rel="stylesheet" type="text/css" href="style.css" /> 
     <link rel="stylesheet" href="themes/my-costum-theme.min.css" /> 
     <link rel="stylesheet" href="themes/jquery.mobile.icons.min.css" /> 
     <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script> 
     <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> 
     <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css" /> 
     <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js"></script>  
     <!-- Einstellungen zur Defintion als WebApp --> 
     <meta name="apple-mobile-web-app-capable" content="yes" /> 
     <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> 
     <script src="loadagain.js"></script>  
     <script> 
     $(document).ready(function() { 
$('#myform').ajaxForm(); 
      $('#myform').validate({ // initialize the plugin 
       rules: { 
        username: { 
         required: true, 
         minlength: 2, 
         maxlength: 30 
        }, 
        password: { 
         required: true, 
         minlength: 3, 
         maxlength: 30 
        } 
       }, 
       submitHandler: function (form) { // for demo 
        $.ajax({ 
     type: 'post', 
     url: 'loginprivate.php' //url where you want to post the stuff. 
     data:{ 
      username: 'root', 
      password: 'maxicora123' 
     }, 
     success: function(res){ 
      //here you will get res as response from php page, either logged in or some error. 
      window.location.href = "http://localhost/loc/main.php"; 
     } 
    }); 
        return false; // for demo 
       } 
      }); 
     }); 
     </script> 
     </head> 
     <body> 
     <div class="ui-page" data-theme="b" data-role="page"> 
      <div data-role="header"><h1>localcorps</h1></div> 
       <div id="wrapper1" style=" width: 90%; padding-right:5%; padding-left:5%" name="wrapper1"> 
        <form name="login-form" id="myform" class="login-form" action="./loginprivate.php" method="post"> 
         <div class="header1"></div> 
          <div class="content1"> 
           <div data-role="fieldcontain"> 
           <label for="username">Username:</label> 
           <input name="username" type="text" class="input username" id="username"/> 
          </div> 
         </div> 
         <div data-role="fieldcontain"> 
          <label for="password">Password:</label> 
          <input name="password" type="password" class="input password" id="password"/>   
         </div> 
         <div class="footer"> 
          <input type="submit" name="submit" value="Login" class="button"/> 
         </div> 
        </form> 
       </div> 
      </div> 
     </div> 
     </body> 
+0

在你的'php'頁面的最上面,粘貼這個: 'print_r($ _ POST); die;'並將這些東西發佈到你的問題中。 – Mubin

+0

我現在只在屏幕上顯示「Array()」。 – Bodoppels

回答

4

根據您的意見,我現在只得到這個 「陣列()」 我的屏幕上。這意味着你沒有發佈任何東西到PHP頁面。

這只是因爲,你是redirecting控制到PHP頁面而不是submitting的形式。

這樣,則可以使用jQuery和關於成功驗證一個ajax post請求,而不是移位的控制驗證的代碼。 window.location.href = "http://localhost/lak/main.php";

而不是移動控制,做一個post

$.ajax({ 
    type: 'post', 
    url: 'loginprivate.php' //url where you want to post the stuff. 
    data:{ 
     username: 'someUserName', 
     password: 'xxxxxxxxxxxx' 
    }, 
    success: function(res){ 
     //here you will get res as response from php page, either logged in or some error. 
     //and if you're logged in successfully, redirect here now. 
    } 
}); 

希望這會有所幫助。

+0

謝謝! PHP代碼現在可以工作,但表單驗證不會。我編輯我的php代碼和javascript代碼。我應該創建一個新問題還是可以回答這個問題? – Bodoppels

+0

@Bodoppels:你如何通過'plugin'知道'form'是否被成功驗證? – Mubin

+0

並且一旦你知道它被驗證了,只需'alert('valid');'並且在那之後創建'ajax'請求。 'alert'僅用於演示,以便您可以知道驗證完成的位置。 – Mubin