1

I am trying to do a simple insert into my database from my phonegap app but all it does when call the ajax function is return the whole save.php file to my success function. Nothing is added to the database and I tried a second tutorial but the same thing happened. I have tried changing the versions of jQuery and jQuery mobile but didn't change anything. I'm new to working with phonegap and jQuery so any help would be appreciated.

Screenshot of returned php file in alert

HTML:

        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript" src="js/jquery.mobile-1.3.2.min.js"></script>
        <link rel="stylesheet" href="css/jquery.mobile-1.3.2.min.css" />
        <script src="js/jquery-1.5.min.js"></script>
        <script src="js/post.js"></script>

     <title>jQuery form post</title>




    <script>
        function onBodyLoad() {     
            document.addEventListener("deviceready",onDeviceReady,false);
        }
    </script>

    <style>
        label, b {
            display: block;
        }
    </style>
</head>
<body onload="onBodyLoad()">

<div id="landmark-1" data-landmark-id="1">
    <form>
        <label for="email">
            <b>Email</b>
            <input type="email" id="email" name="email">
        </label>

        <label for="comment">
            <b>Comment</b>
            <textarea id="comment" name="comment" cols="30" rows="10"></textarea>
        </label>

        <input type="submit" value="Save">
    </form>
</div>

</body>

Javascript:

$(document).bind('deviceready', function(){
    $(function(){
        $('form').submit(function(){
            var landmarkID = $(this).parent().attr('data-landmark-id');
            var postData = $(this).serialize();

            $.ajax({
                type: 'POST',
                data: postData+'&lid='+landmarkID,
                //change the url for your project
                url: 'save.php',
                success: function(data){
                    //console.log(data);
                    alert('Your comment was successfully added ' + data);
                },
                error: function(e){
                    //console.log(data);
                    alert('There was an error adding your comment');
                }
            });

            return false;
        });
    });
});

PHP:

<?php
$server = "xxxxx";
$username = "xxxxx";
$password = "xxxx";
$database = "xxxx";
$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());
mysql_select_db($database, $con);
$locationID = $_POST["lid"];
$email = mysql_real_escape_string($_POST["email"]);
$comment = mysql_real_escape_string($_POST["comment"]);
$sql = "INSERT INTO comments (location_id, email, comment) ";
$sql .= "VALUES ($locationID, '$email', '$comment')";
if (!mysql_query($sql, $con)) {
    die('Error: ' . mysql_error());
} else {
    echo "Comment added";
}
mysql_close($con);
?>

Screenshot of returned php file in alert

2 Answers 2

1

It sounds like you need to configure your server to actually run PHP.
What does a simple 'hello world' php file do?
Try making a file containing this, and tell me what you get please:

<?PHP echo "Hello World"; ?>
Sign up to request clarification or add additional context in comments.

5 Comments

I tried it there and it just returned "<?PHP echo "Hello World"; ?>" to the success function as the data parameter. I am running the php file locally on my computer would that be affecting it running on the phone? I tried running it through an aws virtual machine but that was throwing an error.
I meant, I want you to make a file, like "helloWorld.php". Then, just go to that file from a browser. So if your server is 105.12.111.18, you'd go to 105.12.111.18/helloWorld.php, and then if you see anything but "Hello World", it means your server isn't processing PHP files.
I think you are right about the server. I have since added xampp to my computer and the helloWorld.php file is working in my browser. I then tried running the above code again this time with the php file saved in the htdocs folder but when I try to access the php file the browser just refreshes. While nothing is added to the database. Have you any idea what would cause this?
@Antonio you need to call jquery's "preventDefault" method on the event, otherwise submitting a form will always refresh the page.
Yeah that worked, thanks for that was completely lost there
0

I recommend that you create static query on PHP, replace:

$sql .= "VALUES ($locationID, '$email', '$comment')";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.