0

我开始将我的简单 Ajax-MySql-PHP 程序部署到 Ajax-MySql-PHP-PhoneGap 中,但问题是它没有插入。

这是我的 testjs.html

  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Insert title here</title>

  <script type="text/javascript" charset="utf-8" src="cordova-2.7.0.js"></script>

  <script type="text/javascript" src="js/jquery-1.10.1.js"></script>
  <script type="text/javascript" src="js/jquery-1.10.1.min.js"></script>

  <script>
  document.addEventListener("deviceready", onDeviceReady, false);


  function onDeviceReady() {
console.log("Hello ");
    $("#button").click(function(evt){
    var name = $("#name").val();
    var message = $("#message").val();
    var sendData = { "name": name, "message": message };

    $.ajax({
        type: "POST",
        url: "http://localhost/webs/main/ajax/process.php",
        data: sendData,
        success: function(data) {
            log.console(data);
             $("#info").html(data);

        }

    });

     alert("Hello ");
     alert($("#test").val());
     alert($("#name").val());
     alert($("#message").val());
     return false;


  });
  }

  </script>

  </head>
  <body>
  <form >
  name : <input type="text" name="name" id="name"/>
           </br>
   message : <input type="text" name="message" id="message" />
           </br>
  <input type="text" id="test" name="test" />
  <input type="button" id="button" value="Ajax Submit">
 <div id="info"></div>   <span id="msg"></span>
 </form>
 </body>
 </html>

这是我的 process.php

 <?php

$host = "localhost";
$user = "root";
$pass = "";
$dbname = "comments";
$dsn = "mysql:host=$host; dbname=$dbname";

$pdo = new PDO($dsn, $user, $pass);

$name=$_POST["name"];
$message=$_POST["message"];
try{
$sql = "INSERT INTO comment2 (name,message) VALUES ('$name' , '$message')";
$pdo->exec($sql);
  echo "Your comment has been sent";
 } catch (PDOException $e) {
die('Opps' .$e->getMessage());
 }


?>

这是我的 LogCat

  06-11 18:38:24.652: E/Trace(790): error opening trace file: No such file or  
  directory (2)
  06-11 18:38:25.021: I/CordovaLog(790): Changing log level to DEBUG(3)
  06-11 18:38:25.021: I/CordovaLog(790): Found preference for useBrowserHistory=true
  06-11 18:38:25.091: D/CordovaLog(790): Found preference for useBrowserHistory=true
  06-11 18:38:25.091: I/CordovaLog(790): Found preference for exit-on-suspend=false
  06-11 18:38:25.091: D/CordovaLog(790): Found preference for exit-on-suspend=false
  06-11 18:38:25.091: D/DroidGap(790): DroidGap.onCreate()
  06-11 18:38:25.241: D/dalvikvm(790): GC_FOR_ALLOC freed 70K, 8% free 2480K/2672K, 
  paused 41ms, total 52ms
  06-11 18:38:25.251: I/dalvikvm-heap(790): Grow heap (frag case) to 3.144MB for  
  635812-byte allocation
  06-11 18:38:25.342: D/dalvikvm(790): GC_FOR_ALLOC freed 1K, 6% free 3099K/3296K, 
  paused 94ms, total 94ms
  06-11 18:38:25.441: D/dalvikvm(790): GC_CONCURRENT freed <1K, 6% free 3110K/3296K,  
  paused 4ms+11ms, total 97ms
  06-11 18:38:25.601: D/CordovaWebView(790): CordovaWebView is running on device made  
  by: unknown
  06-11 18:38:25.635: D/JsMessageQueue(790): Set native->JS mode to 2
  06-11 18:38:25.642: D/DroidGap(790): DroidGap.init()
  06-11 18:38:25.692: D/CordovaWebView(790): >>> loadUrl(file:///android_asset
 /www/testjs.html)
  06-11 18:38:25.702: D/PluginManager(790): init()
  06-11 18:38:25.753: D/CordovaWebView(790): >>> loadUrlNow()
  06-11 18:38:25.753: D/DroidGap(790): Resuming the App
  06-11 18:38:25.832: D/SoftKeyboardDetect(790): Ignore this event
  06-11 18:38:26.002: D/gralloc_goldfish(790): Emulator without GPU emulation detected.
  06-11 18:38:26.222: D/SoftKeyboardDetect(790): Ignore this event
  06-11 18:38:26.782: D/DroidGap(790): onMessage(onPageStarted,file:///android_asset
  /www/testjs.html)
  06-11 18:38:33.262: D/CordovaNetworkManager(790): Connection Type: 3g
  06-11 18:38:33.262: D/DroidGap(790): onMessage(networkconnection,3g)
  06-11 18:38:33.337: D/CordovaNetworkManager(790): Connection Type: 3g
  06-11 18:38:33.381: D/DroidGap(790): onMessage(spinner,stop)
  06-11 18:38:33.492: D/TilesManager(790): Starting TG #0, 0x2a259520
  06-11 18:38:33.582: D/Cordova(790): onPageFinished(file:///android_asset 
  /www/testjs.html)
  06-11 18:38:33.582: D/DroidGap(790): onMessage(onPageFinished,file:///android_asset
  /www/testjs.html)
  06-11 18:38:33.642: D/CordovaLog(790): Hello 
  06-11 18:38:33.642: I/Web Console(790): Hello  at file:///android_asset 
   /www/testjs.html:17
  06-11 18:38:38.382: D/SoftKeyboardDetect(790): Ignore this event
  06-11 18:38:39.522: D/SoftKeyboardDetect(790): Ignore this event
  06-11 18:38:40.302: D/SoftKeyboardDetect(790): Ignore this event
  06-11 18:38:46.661: D/dalvikvm(790): GC_CONCURRENT freed 253K, 11% free 3269K/3644K, 
  paused 6ms+40ms, total 102ms
  06-11 18:38:47.061: I/Choreographer(790): Skipped 46 frames!  The application may be 
  doing too much work on its main thread.
  06-11 18:38:49.091: I/Choreographer(790): Skipped 95 frames!  The application may    be    doing too much work on its main thread.
  06-11 18:38:50.651: I/Choreographer(790): Skipped 74 frames!  The application may be 
  doing too much work on its main thread.
  06-11 18:38:52.081: D/dalvikvm(790): GC_CONCURRENT freed 24K, 5% free 3654K/3812K, 
  paused 70ms+86ms, total 191ms
  06-11 18:38:52.552: I/Choreographer(790): Skipped 85 frames!  The application may be 
  doing too much work on its main thread.

显示所有警报意味着 onDeviceReady() 正在运行。在我部署到 phoneGap 之前,此代码也适用于网络。我在这里错过了什么吗?谢谢你。

4

1 回答 1

0

正如我之前评论过的,由于您部署了应用程序,因此无法引用process.php通过 localhost 调用的 Php 页面。您必须指向托管该 Php 页面的服务器的地址。所以你的代码看起来像这样:

    $.ajax({
    type: "POST",
    url: "http://<INSERT YOUR SERVER IP ADDRESS OR DOMAIN HERE>/webs/main/ajax/process.php",
    data: sendData,
    success: function(data) {
        log.console(data);
         $("#info").html(data);

    }
   });

当然,假设您的服务器中具有相同的文件夹/目录结构。

另外,请注意@Marc B 关于 SQL 注入的评论。确保在将查询发送到数据库之前清理用户输入。这是一篇关于如何使用 Php 清理 mysql 输入的好帖子。

希望这可以帮助

于 2013-06-11T05:31:11.067 回答