2015-01-03 80 views
0

我目前正在使用Cordova構建一個移動應用程序,而且我無法獲得使用我的代碼的基本測試登錄功能。我不知道爲什麼這不起作用,我不熟悉我必須使用php的複雜方式,所以我甚至不知道如何從代碼的該部分獲得適當的錯誤報告。如果有人能幫助我,我會非常感激。下面在使用AJAX POST POST數據庫時遇到問題

代碼(我已經空白了一些細節有星號。

HTML

<html> 
    <head> 
     <meta charset="utf-8" /> 
     <meta name="format-detection" content="telephone=no" /> 
     <meta name="msapplication-tap-highlight" content="no" /> 
     <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 --> 
     <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" /> 
     <link rel="stylesheet" type="text/css" href="css/index.css" /> 
     <title>Hello World</title> 
    </head> 
    <body> 
    <div class="app"> 
      <form id="Loginform"> 
      <p><label>username:</label> 
      <input type="text" id="unl" /></p> 
      <p> 
       <label>Password:</label> 
      <input type="text" id="pwl" /></p><p> 

      </p> 
      <p><input type="submit" id="login" value="Login" onClick="logIn();" /> 
      </p></form> 

     <form id="Registerform"> 
      <p><label>username:</label> 
      <input type="text" id="unr" /></p> 
      <p> 
       <label>Password:</label> 
      <input type="text" id="pwr" /></p><p> 
      <label>Re-type Password 
       :</label> 
      <input type="text" id="pw2r" /> 
      </p> 
      <p> 
      <input type="submit" id="register" value="Register" onClick="regisTer();" /></p> 

     </form> 
     <input type="button" id="check" value="check" onClick ="checkUn();"> 
    </div> 


    <script type="text/javascript" src="cordova.js"></script> 
    <script type="text/javascript" src="js/localstorage.js"></script> 
    <script type="text/javascript" src="js/logreg.js"></script> 
    <script type="text/javascript" src="js/camera.js"></script> 
    <script type="text/javascript" src="js/jquery-2.1.3.min.js"></script> 
    <script type="text/javascript" src="js/plugins/LaunchMyApp.js"></script> 
    <script type="text/javascript" src="js/index.js"></script> 
    </body> 
</html> 

logreg.js

 function logIn() { 
    alert("1"); 
} 

function regisTer() { 
    var un = $("#unr").val(); 
    var pw = $("#pwr").val(); 
    var pw2 = $("#pw2r").val(); 

    if (pw!='' && pw2!='' && un!='') { 

     if (pw == pw2) { 
      alert("run post"); 
      $.post("http://cs12ars.icsnewmedia.net/Media/register.php", 
      { 
       name:un, pass:pw 
      }, function() { 
       alert("success"); 
       }) 
       .done(function() { 
       alert("second success"); 
       }) 
       .fail(function() { 
       alert("error"); 
       }) 
       .always(function() { 
       alert("finished"); 
      }); 


     } else { 
      alert("both passwords must match"); 
     } 
    } else { 
     alert("Please fill in all fields."); 
    } 


} 

register.php(在服務器上,同一臺服務器數據庫)

<?php 

//clean input 
function clean_string($db_server = null, $string){ 
$string = trim($string); 
$string = utf8_decode($string); 
$string = str_replace("#", "&#35", $string); 
$string = str_replace("%", "&#37", $string); 
if (mysqli_real_escape_string($db_server, $string)) { 
$string = mysqli_real_escape_string($db_server, $string); 
} 
if (get_magic_quotes_gpc()) { 
$string = stripslashes($string); 
} 
return htmlentities($string); 
} 

//encrypt data 
function salt ($string) { 
     $salt1 = 'gds54d'; 
     $salt2 = '54h6'; 
     $salted = md5 ("$salt1$string$salt2"); 
     return $salted; 
} 

?> 

<?php 

$db_hostname = 'localhost'; 
$db_database = '****'; 
$db_username = '****'; 
$db_password = '****'; 
$db_status = 'not initialised'; 
$str_result = ''; 
$str_options = ''; 
$db_server = mysqli_connect($db_hostname, $db_username, $db_password); 
$db_status = "connected"; 
//connect to the database 
mysqli_select_db($db_server, $db_database); 
$name = $_POST['name']; 
$pw = $_POST['pass']; 
$password = salt($pw); 

$query = "INSERT INTO `bla` (`bla`, `test`) VALUES (NULL, '$name');"; 

mysqli_query($db_server, $query); 
mysqli_close($db_server); 
?> 
+0

那麼,顯然有很多事情可能會出錯。首先,確保在調用_ $。post_之前添加_alert_來發送您的POST。如果觸發了這種情況,請爲[$ jQuery文檔](http://api.jquery.com/jquery.post/)中爲_ $。post_添加回調以獲取成功和失敗的回調,並在這些警報中查看是否執行哪一個。從這些開始,我會在更多後幫助你。 –

+0

我已經添加了各種警報的錯誤回調,以及在帖子運行之前的回調。崗位發射前警報(跑位),但沒有任何回調;既不是成功的,也不是錯誤的。不知道這是否意味着我做錯了什麼或者是否有幫助。 – AlexRS

回答

0

我完全不知道爲什麼,但是使用$ .post更改爲$ .ajax並將asnyc設置爲false使其開始工作。

$.ajax({ 
    type: "POST", 
    url: "http://cs12ars.icsnewmedia.net/Media/register.php", 
    data: {name:un, pass:pw}, 
    async: false 
});