weixin_33735676 2017-09-11 01:43 采纳率: 0%
浏览 6

ajax php获取行值

how can i get the value of the rows every time i clicked the correspondent button heres my code for form

      <form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
          <tbody>
              <?php
              while ($reserve=mysqli_fetch_array($record)) {
                echo "<tr>";
                echo "<td>".$reserve['id']."</td>";
                echo "<td>".$reserve['room']."</td>";
                echo "<td>".$reserve['status']."</td>";
                echo "<td>".$reserve['client']."</td>";
                echo "<td>".$reserve['dateandtime']."</td>";
                echo "<td>₱ ".$reserve['balance']."</td>";
                echo "<td>".$reserve['additional']."</td>";
               ?>
                  <td style="text-align: center;">
                     <div class="ui buttons">
                      <button type="submit" name="approved" class="Huge ui green button" id="update">Approved</button>
                      <button type="submit" name="cancel" class="Huge ui red button">Cancel</button>
                      <button type="submit" name="checkin" class="Huge ui teal button">Check In</button>
                      <button type="submit" name="checkout" class="Huge ui violet button">Check Out</button>
                    </div>
                  </td>
               </tr>
          <?php } ?>
          </tbody>

what i want is to get the value of the rows every time i clicked the correspondent button so that i can update it

  • 写回答

1条回答 默认 最新

  • weixin_33709590 2017-09-11 02:03
    关注

    Try this: Add an attribute reserveId for update button and use the jQuery script to get the Id as per the current row when you will click on update button.

          <tbody>
              <?php
              while ($reserve=mysqli_fetch_array($record)) {
                echo "<tr>";
                echo "<td>".$reserve['id']."</td>";
                echo "<td>".$reserve['room']."</td>";
                echo "<td>".$reserve['status']."</td>";
                echo "<td>".$reserve['client']."</td>";
                echo "<td>".$reserve['dateandtime']."</td>";
                echo "<td>₱ ".$reserve['balance']."</td>";
                echo "<td>".$reserve['additional']."</td>";
               ?>
                  <td style="text-align: center;">
                     <div class="ui buttons">
                      <button type="submit" reserveId="<?=$reserve['id']?>"  name="approved" class="Huge ui green button" id="update">Approved</button>
                      <button type="submit" name="cancel" class="Huge ui red button">Cancel</button>
                      <button type="submit" name="checkin" class="Huge ui teal button">Check In</button>
                      <button type="submit" name="checkout" class="Huge ui violet button">Check Out</button>
                    </div>
                  </td>
               </tr>
          <?php } ?>
          </tbody>
    
    
    <script type="text/javascript">
        $(document).on('click', "#update", function(e){
            e.preventDefault();
    
            var reserve_id = $(this).attr('reserveId');
            alert( 'Your reserve_id is: '+ reserve_id); 
    
            jQuery.ajax({
                type : "post",
                dataType : "json",
                url : 'your-file.php',
                statusCode: {
                     500: function() {
                     alert(" 500 data still loading");
                     console.log('500 ');
                 }
            },
    
            data : { reserve_id : reserve_id, action: 'update'},
    
            error: function(xhr, status, error) {
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message);
            },
            success: function(response) {
                alert(response.res_message);    
            },
          }); 
        });
    </script>
    

    your-file.php

    <?php
        if ( isset($_POST['action']) ) {
    
            if ($_POST['action'] == 'update')
            {
                $reserve_id = $_POST['reserve_id'];
                #update code here
    
                $data['res_message'] == "Record updated for the id: $reserve_id";
            }
            else
            {
                #code for another action for example delete
                $data['res_message'] == "Invalid request";
            }
    
            echo json_encode($data); die();
    
        }
        else
        {
            $data['res_message'] == "Invalid action request";
            echo json_encode($data); die();
        }
    ?>
    
    评论

报告相同问题?

悬赏问题

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