在Phonegap android中无法使用AJAX, jQuery和php在mysql中存储数据


Unable to store data in mysql using AJAX, jQuery and php in Phonegap android

我正在使用phonegap开发一个应用程序。我试图将mysql数据库连接到phonegap,以使用Ajax, php和jquery从html5页面保存数据。但是我无法在mySql数据库中保存数据。下面的代码有什么问题吗??下面是我的Html和php代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>MySQL Database with Phonegap</title>

        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script src="//code.jquery.com/jquery-1.10.2.js"></script>
        <script>
            $('form').submit(function()
            {
                var postData = $(this).serialize();
                $.ajax(
                {
                    type: 'POST',
                    data: postData,
                    url: 'http://localhost/Save.php',
                    success: function(data)
                    {
                        console.log(data);
                        alert('Your data was successfully added');
                    },
                    error: function()
                    {
                        console.log(data);
                        alert('There was an error adding your comment');
                    }
            });
                return false;
        });
        </script>
    </head>
    <body>
            <h1 align="center">Database Connectivity</h1><br><br><br>
            <div align="center">
                <form>
                <label><b>First Name :</b></label><br>
                <input type="text" id="fname" name="fname"/><br>
                <label><b>Last Name :</b></label><br>
                <input type="text" id="lname" name="lname"/><br>
                <label><b>Email ID :</b></label><br>
                <input type="email" id="email" name="email"/><br><br>
                <input type="submit" value="SUBMIT"/>&nbsp;&nbsp;&nbsp;
                <input type="reset" value="RESET"/>
                </form>
            </div>
    </body>
</html>

Save.php

<?php
    $host='localhost';
    $uname='root';
    $pwd='xyz';
    $db='Sample';
    $con=mysql_connect($host,$uname,$pwd) or die("Connection Failed");
    mysql_select_db($db,$con) or die("database selection failed");
    $fname = mysql_real_escape_string($_POST['fname']);
    $lname = mysql_real_escape_string($_POST['lname']);
    $email = mysql_real_escape_string($_POST['email']);
    //echo "Hello";
    $flag['code']=0;
    if($r=mysql_query("INSERT INTO Login (FirstName, LastName, Email) VALUES ('".$fname."','".$lname."','".$email."')",$con))
    {
        $flag['code']=1;
        echo "okk...";
    }
    print(json_encode($flag));
    mysql_close($con);
?>
$.ajax({
    url: "https://yourphpfilehere.php?PARAMETERSHERE=" + javascriptvariable,
    success: function(res) {
        alert(res);
    }
});

,在你的php文件中,我建议你这样使用:

$myvariable = $_REQUEST['URLPARAMETER'];
//do the mysql statement,proccess here . . .

你应该从php脚本中使用$_REQUEST方法访问变量,因为你要在那个php文件上请求。不要让你的代码太复杂