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);
?>