ajax php获取行值

时间:2017-09-11 01:43:35

标签: 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>

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

1 个答案:

答案 0 :(得分:2)

尝试此操作:为更新按钮添加属性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>

您-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();
    }
?>