javascript - ajax php 获取行值

标签 javascript php jquery ajax

如何在每次单击相应按钮时获取行的值 这是我的表单代码

      <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>

我想要的是每次单击相应按钮时获取行的值,以便我可以更新它

最佳答案

试试这个:为更新按钮添加一个属性reserveId,并在单击更新按钮时使用 jQuery 脚本根据当前行获取 Id。

      <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>

你的文件.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();
    }
?>

关于javascript - ajax php 获取行值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46147155/

相关文章:

javascript - jquery悬停效果

javascript - 提交表单并关闭 Fancybox,Javascript

php - 在类内部的函数内部的此处文档中使用 PHP 变量

javascript - 如何使加载动画 gif 在 getjson 调用完成后不可见并显示 jw 播放器?

javascript - 从 Ajax 调用返回 php 错误

javascript - jQuery.kinetic - 如何在页面加载时预滚动到某个位置

javascript - 将图钉添加到单击的位置

php - 在特定时间戳更新信息

php - Twilio 获取帐户上所有已购买的号码

javascript - 如何单独用jQuery解析xml属性?