duanjiwei1283 2015-03-27 14:08
浏览 24

发送和接收数据? Ajax,Jquery和PHP

What I would like to create is a attendance button. Once the user clicks on the button +1 would be added to the total number of people attending. Is there any way of doing this without refreshing the page? I some how need to connect it to the database to add +1.

I've heard AJAX could be used and PHP but I have limited knowledge in these languages. Any help please? Here is what I have so far...

$(document).on('click', 'button', function(){
    var parente = $(this).prev().text();
    var current_val = parseInt($(this).next().text());
    current_val++;
    $(this).next().text(current_val +' Going '+parente);
    // Here you can make ajax request to send the number of people who is going to those events
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
    <head></head>
    <body>
        <div class="my_event">
            <span>Event 1</span>
            <button>I wanna go</button>
            <div class="output_event">0</div>
        </div>
    </body>
</html>
<script src="button.js"></script>
  • 写回答

3条回答 默认 最新

  • dongshi4078 2015-03-27 14:15
    关注

    first step: you must get value from input or other event

    second step: send that data using ajax method, like this:

    $.ajax({
                    url: '/Assessment/Save',
                    type: 'POST',
                    data: {A:QNo},
                    async: false,
                    success: function (data) {
                        //alert("Test: " + data.result);
                        if (data.result == "T")
                            flag = true;  
                          successCallback(flag);
                    },
                    error: function (req, status, error) {
                        //alert("R: " + req + " S: " + status + " E: " + error);
                        alert('Unable to connect server!'); 
                       flag = false;  
                        successCallback(flag);                 
                    }
                }); 
    
    1. And PHP, example simple maybe like that:

      // to do with $data
      
      $result = "your result";
      
      echo json_encode($result);
      
      ?  >
      
    2. and last, manipulation object html in success or complete ajax:

      var parente = $(this).prev().text(); var current_val = parseInt($(this).next().text()); current_val++; $(this).next().text(current_val +' Going '+parente);

    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制