0

I started to deploy my simple Ajax-MySql-PHP Program into Ajax-MySql-PHP-PhoneGap but the problem is it's not inserting.

Here is my 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>

and here is my 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());
 }


?>

here's my 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.

All the alerts is showing up means that onDeviceReady() is functioning. This codes is also working in web before i deployed in phoneGap. Am I missing something here? Thank you.

miles
  • 225
  • 3
  • 10
  • 20
  • Hi, I am not familiar with phonegap but I would suggest you check if you need to change the reference to your Php page before deploying the application to phoneGap. In your `$.ajax POST` you're pointing to Localhost (http://localhost/webs/main/ajax/process.php) and probably this is wrong – juanreyesv Jun 11 '13 at 03:46
  • Thank you so how am i going to shift my $.ajax POST to correct ajax post. Are you talking about the use of XMLHttpRequest ? – miles Jun 11 '13 at 04:16
  • Just try replacing the localhost in the url inside the `$.ajax POST` with the address of the server where you have your php page (process.php) hosted. – juanreyesv Jun 11 '13 at 04:26
  • 1
    Lovely [SQL injection attack](http://bobby-tables.com) holes in your code. Enjoy having your server pwn3d. – Marc B Jun 11 '13 at 04:35
  • I replaced localhost into my ipaddress and its now working.. thank you! – miles Jun 11 '13 at 04:43

1 Answers1

0

As I commented before, since you deployed your application, you can't refer the Php page called process.php through localhost. You have to point to the address of the server hosting that Php page. So your code will looking like this:

    $.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);

    }
   });

Of course assuming you have the same folder/directory structure in your server.

Also, pay attention to @Marc B comment about SQL Injection. Make sure you sanitize user inputs before sending the query to the database. Here is a good post on how to sanitize mysql inputs with Php.

Hope this helps

Community
  • 1
  • 1
juanreyesv
  • 853
  • 9
  • 22