在Phonegap项目中组合Ajax,Jquery,mysql,php是行不通的

时间:2013-11-23 21:31:17

标签: php jquery mysql ajax cordova

我尝试使用以下代码将数据发送到我的数据库,但它不起作用。代码是在此示例(http://samcroft.co.uk/2012/posting-data-from-a-phonegap-app-to-a-server-using-jquery/

之后编写的

如果有人知道什么是错的......我感谢任何帮助。

HTML表单

<form>
    <fieldset data-role="controlgroup">
        <legend>Pick your team:</legend>
        <input type="radio" name="team_name" id="red" value="on" checked="checked">
        <label for="red">Team Red</label>
        <input type="radio" name="team_name" id="blue" value="off">
        <label for="blue">Team Blue</label>       
    </fieldset>
    <fieldset data-role="controlgroup" data-type="horizontal">
        <legend>Choose your number:</legend>
        <input type="radio" name="player_number" id="1" value="on" checked="checked">
        <label for="1">1</label>
        <input type="radio" name="player_number" id="2" value="off">
        <label for="2">2</label>
        <input type="radio" name="player_number" id="3" value="off">
        <label for="3">3</label>
        <input type="radio" name="player_number" id="4" value="off">
        <label for="4">4</label>
        <input type="radio" name="player_number" id="5" value="off">
        <label for="5">5</label>
        <input type="radio" name="player_number" id="6" value="off">
        <label for="6">6</label>
        <input type="radio" name="player_number" id="7" value="off">
        <label for="7">7</label>
        <input type="radio" name="player_number" id="8" value="off">
        <label for="8">8</label>
        <input type="radio" name="player_number" id="9" value="off">
        <label for="9">9</label>
        <input type="radio" name="player_number" id="10" value="off">
        <label for="10">10</label>
    </fieldset>
    <label for="player_name">Your name:</label>
    <input type="text" name="player_name" id="player_name" value="">         
    <input type="submit" value="Submit">
</form>

Jquery / Ajax代码

$('form').submit(function(){
    var postData = $(this).serialize();

    $.ajax({
        type: 'POST',
        data: postData,
        url: 'http://www.kauwenberg.com/Commander/core/handleplayer_2.php', // 2 !!
        succes: function(data){
            console.log(data);
            alert(data);
            },
        error: function(){
            console.log(data);
            alert(data);
            }
    });

    return false;
});

http://www.kauwenberg.com/Commander/core/handleplayer_2.php上的PHP代码

<?php
$server = "localhost";
$username = "MA_SECRET";
$password = "MA_SECRET";
$database = "MA_SECRET";

$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());

mysql_select_db($database, $con);

$team_name = mysql_real_escape_string($_POST["team_name"]);
$player_number = mysql_real_escape_string($_POST["player_number"]);
$player_name = mysql_real_escape_string($_POST["player_name"]);

$sql = "INSERT INTO players (team_name, player_number, player_name) ";
$sql .= "VALUES ($team_name, $player_number, $player_name)";

if (!mysql_query($sql, $con)) {
  die('Error: ' . mysql_error());
} else {
  echo "Comment added";
}

mysql_close($con);
?>

5 个答案:

答案 0 :(得分:1)

$(document).on('submit','form',function(e){
    e.preventDefault();
    var postData = $(this).serialize();
    $.post('http://www.kauwenberg.com/Commander/core/handleplayer_2.php',postData,
    function(data){ 
        alert(data);
    });    
    return false;
});

试试这个

答案 1 :(得分:1)

您可以查看本页的最后一个问题

http://phonegap.com/about/faq/

我建议改用JSON。

检查此链接 http://catchmayuri.blogspot.com/2012/01/working-with-json-phonegap-application.html

答案 2 :(得分:1)

注意:您没有在SQL代码中引用$ team_name,$ player_number和$ player_name,这可能 非常危险 。如果您的查询中的值未被正确引用,则使用mysql_real_escape_string的事实并不重要;它们仍将被解析为普通的SQL。

$sql .= "VALUES ('$team_name', '$player_number', '$player_name')";

这可能不是它目前无法正常工作的原因,但当你开始处理其他事情时,肯定会回复你。

另外,检查您的AJAX请求是否到达服务器。如果您的请求跨越多个域,则可能会被手机的安全政策阻止。

答案 3 :(得分:0)

您可以将提交按钮更改为<input type="button" value="Submit" id="submit_button" />,然后将您的jQuery事件更改为以下内容:

$('#submit_button').bind('click',function(e){

    e.preventDefault();
    var postData = $(this).serialize();

    $.ajax({
        type: 'POST',
        data: postData,
        url: 'http://www.kauwenberg.com/Commander/core/handleplayer_2.php', // 2 !!
        success: function(data){
            console.log(data);
            alert(data);
        },
        error: function(){
            console.log(data);
            alert(data);
        }
    });
    return false; 
});

它将警告从服务器返回的响应将显示在警告对话框中。

答案 4 :(得分:0)

你也有拼写错误:

  

成功:function(data){

&#34;成功&#34;就是这个词。