如何在点击锚标签到php时传递输入框值?

时间:2017-11-19 07:46:06

标签: php html

我想将每行的值传递给php页面以在数据库中更新我只能传递id而不是输入框值

这是表格主体

  <?php
  $cards = mysqli_query($con,"SELECT Username, Card_No, Card_Status FROM 
  smartcard WHERE Card_Status NOT IN (SELECT Card_Status FROM smartcard where Card_Status = 'Issued')");
  ?>
 <tbody>
        <?php
        while ($row = mysqli_fetch_assoc($cards))
        {
            echo '<tr>';
            foreach ($row as $key => $value) {
                echo '<td style="word-wrap: break-word;min-width: 10px;max-width: 300px;">',$value,'</td>';
            }            
            echo '<td><form action="php/issue_card.php" method="POST"><input id="textbox" name="new_status" type="text" class="form-control" required="required"/></form></td>';
            echo "<td><a href='php/issue_card.php?id=".$row['Card_No']."'><button type=\"button\" class=\"btn btn-link\">Status Update</button></a></td>";
            echo '</tr>';
        }
        ?>
 </tbody>

这是php页面issue_card.php

 <?php
  include("connect.php");
  $con = OpenCon();
  $smartcard_no = $_GET['id'];
  $admin_id = $_SESSION['Admin_Id'];
  $new_status = $_POST['new_status'];
  $sql = "UPDATE smartcard SET Card_Status = '$new_status', Admin_Id ='$admin_id' WHERE smartcard.smartcard_no = $smartcard_no";
   mysqli_query($con, $sql);
  CloseCon($con);
  ?>

对于每一行,会有不同的smartcard_no,所以我使用锚标签直接使用GET传递,但不能传递输入框值。

2 个答案:

答案 0 :(得分:1)

      <?php
      $cards = mysqli_query($con,"SELECT Username, Card_No, Card_Status FROM 
      smartcard WHERE Card_Status NOT IN (SELECT Card_Status FROM smartcard where Card_Status = 'Issued')");
      ?>
     <tbody>
<form>
            <?php
            while ($row = mysqli_fetch_assoc($cards))
            {
                echo '<tr>';
                foreach ($row as $key => $value) {
                    echo '<td style="word-wrap: break-word;min-width: 10px;max-width: 300px;">',$value,'</td>';
                }            
                echo '<td><input class="textbox" name="new_status" type="text" class="form-control" required="required"/></td>';
                echo "<td><button class="updateBtn" data-id=".$row['Card_No'].">Status Update</button></a></td>";
                echo '</tr>';
            }
            ?>
     </tbody>


<script>
/*import jquery before using this.*/


$(document).ready(function(){

$('.updateBtn').on('click',function(){
var status=$(this).parent().find('.textbox').val()
var id=$(this).attr('data-id');
sendData(status,id)

})

function sendData(status,id){
$.ajax({
url:'php/issue_card.php',
data:{status:status,id:id},
type:'get' //get or post,
success:function(success){
//callback
},
error:function(error){
//error callback
}
})



}

})


</script>

答案 1 :(得分:0)

以下是第二个想法的快速编写(未经测试)示例 - 其中只有一个表单包含整个表,并且有两个隐藏字段用于您希望POST到PHP的重要数据片段脚本。

表中的每个按钮都分配了一个事件处理程序 - 在这种情况下,事件处理程序将查询DOM以查找同一行中的文本字段,然后分配该值以及按钮所拥有的值{{1}在提交表单之前输入隐藏的输入字段。

data-id