使用Ajax插入mysql数据库的问题

时间:2016-01-09 04:01:40

标签: php jquery mysql ajax cordova

我正在尝试从我的phonegap应用程序进行一个简单的插入我的数据库,但是当调用ajax函数时它所做的就是将整个save.php文件返回到我的成功函数。没有任何东西添加到数据库,我尝试了第二个教程,但同样的事情发生了。我试过改变jQuery和jQuery mobile的版本,但没有改变任何东西。我是使用phonegap和jQuery的新手,所以任何帮助都会受到赞赏。

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 个答案:

答案 0 :(得分:1)

听起来您需要将服务器配置为实际运行PHP 一个简单的“你好世界”是什么? php文件吗?
尝试制作一个包含此文件的文件,并告诉我你得到了什么:

<?PHP echo "Hello World"; ?>

答案 1 :(得分:0)

我建议您在PHP上创建静态查询,替换:

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