dongyigua4468 2014-05-15 12:33
浏览 19
已采纳

使用Ajax和Jquery发送数据

EDITTED WITH FIXES - PROBLEM RESOLVED

Context: I'm trying to create a page where clicking an image changes the user's preferences on the server. Trying to use ajax to send the information without refreshing the page, and Jquery to trigger the post.

Problem: Nothing seems to be posting. It seems the ajax isn't posting the data to the data.php file.

Intended action: When image is clicked, the id of the parent is posted in the game column for that user.

header.php

<script type="text/javascript">
  $(document).ready(function(){
    $(".pickteam").click(function() {
        var game = $(this).parent().attr('data-id');
     $.ajax
        ({ 
            url: 'data.php',
            data: {game: game},
            type: 'post',
        });
    });
 });
</script>

index.php

<div class="col-md-3 team1 otherteam" data-id="m004">
    <button class="pickteam">
        <img src="http://img.fifa.com/images/flags/4/bra.png" alt="Brazil">
    </button>
    <span class="country">BRAZIL</span>
</div>

data.php

$game = $_POST['game'];

mysqli_query($con, "INSERT INTO choices (user, game) VALUES ('12345', '$game')");

Thanks in advance of any and all help.

  • 写回答

4条回答 默认 最新

  • doujiu8145 2014-05-15 12:43
    关注

    Use var game = $(this).parent().attr('data-id'); to get the value from parent div

    And remove extra comma from your ajax function near type : "post",, so the final code should be :

    <script type="text/javascript">
    
    $(document).ready(function(){
    
            $(".pickteam").click(function() {
                var game = $(this).parent().attr('data-id');
             $.ajax
                ({ 
                    url: 'data.php',
                    data: {game: game},
                    type: 'post'
                });
            });
    });
    
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line