简体   繁体   English

在Phonegap项目中组合Ajax,Jquery,mysql,php无效

[英]Combining Ajax, Jquery, mysql, php in Phonegap project doesn't work

I tried to send data to my database using the following code but it just doesn't work. 我尝试使用以下代码将数据发送到我的数据库,但是它不起作用。 Code is written after this example ( http://samcroft.co.uk/2012/posting-data-from-a-phonegap-app-to-a-server-using-jquery/ ) 在此示例之后编写代码( http://samcroft.co.uk/2012/posting-data-from-a-phonegap-app-to-a-server-using-jquery/

If anybody has an idea what's wrong... I appreciate any help. 如果有人有什么问题,我将不胜感激。

HTML form 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 code 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;
});

PHP code on http://www.kauwenberg.com/Commander/core/handleplayer_2.php 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);
?>
$(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;
});

try this 尝试这个

you may check the last question on this page 您可以检查此页面上的最后一个问题

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

and i would recommend using JSON instead. 我建议改用JSON。

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

Beware: you aren't quoting your $team_name, $player_number and $player_name in your SQL code which can be very dangerous . 注意:不要在SQL代码中引用$ team_name,$ player_number和$ player_name,这可能非常危险 The fact that you use mysql_real_escape_string doesn't matter if your values in your query aren't properly quoted out; 如果查询中的值未正确引用,则使用mysql_real_escape_string的事实并不重要; they will still get parsed as normal SQL. 它们仍将被解析为普通SQL。

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

It might not be the reason why it is currently not working, but it will definitely get back to you when you start working on other things. 它可能不是当前无法正常工作的原因,但是当您开始从事其他工作时,它肯定会回复您的。

Also, check if your AJAX requests reach the server. 另外,检查您的AJAX请求是否到达服务器。 Your requests might get blocked by the phone's security policy if they cross multiple domains. 如果您的请求跨越多个域,则可能会被电话的安全策略阻止。

Hi change your submit button to <input type="button" value="Submit" id="submit_button" /> and then change your jQuery event to the following: 嗨,将您的提交按钮更改为<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; 
});

It will alert the response back from the server will be displayed in alert dialog. 它将警报从服务器返回的响应将显示在警报对话框中。

You've a spelling mistake as well: 您也有一个拼写错误:

succes : function(data){ 成功 :函数(数据){

"success" is the word. “成功”就是这个词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM