2017-03-27 32 views
0

我正在開發一個簡單的應用程序,以使用phonegap保存mysql數據庫(xampp)上的記錄。不過,我正在某處被困住。 我的代碼一直寫「連接...」永遠不保存數據庫上的記錄。我研究了所有,但我似乎無法解決問題請能在某處向我顯示我的代碼中的錯誤?謝謝 也許它的網址,我不知道, 注:我的網址是從phonegap服務器。在手機上使用jquery在Xampp上插入數據

index.html 
      <link rel="stylesheet" type="text/css" href="css/index.css" /> 
     <title>Hello World</title> 
     </head> 

    <body> 
    <script src="jquery-3.1.1.min.js"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
    <script type="text/javascript" src="cordova.js"></script> 
    <script type="text/javascript" src="js/index.js"></script> 
    <script type="text/javascript"> 
    app.initialize(); 
    $(document).ready(function() 
     { 
    $("#insert").click(function(){ 
     var firstname=$("#firstname").val(); 
     var lastname=$("#lastname").val(); 
     var phone=$("#phone").val(); 
     var dataString="firstname="+firstname+"&lastname="+lastname+"&phone="+phone+"&insert="; 
     if($.trim(firstname).length>0 & $.trim(lastname).length>0 & $.trim(phone).length>0) 
      { 

     $.ajax({ 
     type: "POST", 
     url: "http://10.250.216.195:3000/insert.php", 
     data: dataString, 

     crossDomain: true, 
     cache: false, 
     beforeSend: function(){ $("#insert").val('Connecting...');}, 
     success: function(data){ 
      if(data=="success"){ 
       console.log(data); 
       alert("inserted"); 
       $("insert").val('submit'); 
      } 
      else if (data=="error"){ 
       console.log(data); 
       alert("error"); 
      } 
     } 

    }); 

    } return false; 
    }); 
     } 



    ); 

     </script> 
    <div data-role ="page" id ="page1"> 
    <div data-role ="header"> 
     <h1>Hello World</h1> 
     <center><h3>Register</h3> 
     <input type="hidden" id="id" value=""/> 
     <label for ="Firstname" class="ui-hidden-accessible"></label> <input type = "text" id="firstname" placeholder ="firstname" required="required"><br> 
     <label for ="Lastname" class="ui-hidden-accessible"></label> <input type = "text" id="lastname" placeholder ="lastname" required="required"><br> 
     <label for ="Phone" class="ui-hidden-accessible"></label>  <input type = "text" id="phone" placeholder ="Phone" required="required"><br> 
     <input type="button" value="insert" id = "insert"></center> 


    </div> 


insert.php 
    <?php 
    include "dbconfig.php"; 
    if(isset($_POST['insert'])) 
    { 
    $first=$_POST['firstname']; 
    $last=$_POST['lastname']; 
    $phone=$_POST['phone']; 

    $first = mysqli_real_escape_string($con,$_POST['firstname']); 
    $last = mysqli_real_escape_string($con,$_POST['lastname']); 
    $phone = mysqli_real_escape_string($con,$_POST['phone']); 

    $q=mysqli_query($con,"INSERT INTO phone (firstname,lastname,phone) VALUES ('$first','$last','$phone')"); 
    if($q) 
echo "success"; 
    else 
    echo "error"; 
    } 
    ?> 
+0

我認爲沒有人對此有任何意見:( – masquellett

回答

0

如果您使用我假設服務器的XAMPP服務器是您正在開發在同一臺計算機上。

如果是使用谷歌Chrome瀏覽器的情況下測試的PhoneGap應用程序在瀏覽器中,並改變你的網址:

http://10.250.216.195/insert.php 

端口3000是PhoneGap的構建測試。上面的URL應該可以正常工作。

+0

是的,我使用的是同一臺計算機,我沒有將端口3000更改爲上面提到的url,但仍然無法正常工作。 – masquellett