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 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线