PHP - 将隐藏值传递到 jquery 中

PHP - pass hidden value into the jquery

本文关键字:jquery 值传 隐藏 PHP      更新时间:2023-09-26
<html>
<head>
<link rel="stylesheet" href="js/jquery-ui-themes-1.11.1/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.11.1/jquery-ui.js"></script>
<script>
$(document).ready(function(){
    $(".buttonsPromptConfirmDeleteDepartment").click(function(){
        var departmentID = $('input#departmentID').val();
        alert(departmentID);
    });
});
</script>
</head>
<body>
<?php
//db connection
$query = "SELECT * 
          FROM department 
          ORDER BY dept_ID ASC";
$result = mysqli_query($dbc, $query);
$total_department = mysqli_num_rows($result);
if($total_department > 0)
{
?>
<table width="600" border="1" cellpadding="0" cellspacing="0" style="border-collapse:collapse">
    <tr>
      <td width="80" align="center">ID</td>
      <td width="300" align="center">Department</td>
      <td width="220" align="center">Action</td>
    </tr>   
<?php        
    while($row = mysqli_fetch_array($result))
    {
?>
      <tr>
        <td align="center"><?php echo $row['dept_ID']; ?></td>
        <td align="center"><?php echo $row['dept_name']; ?></td>
        <td>
          <button class="buttonsPromptConfirmDeleteDepartment">Delete</button>
          <input type="hidden" id="departmentID" value="<?php echo $row['dept_ID']; ?>" />    
        </td>
      </tr>
<?php
   }
?>
  </table> 
<?php
}
?>

部门表
       dept_ID dept_name
                    1 帐户
                    2 财务                    3 市场营销

假设我的部门表只有 3 条记录.
我的要求如下:
- 单击第一个删除按钮,显示部门 ID = 1
- 单击第二个删除按钮,显示部门 ID = 2
- 单击第三个删除按钮,显示部门ID = 3

但是,从我的代码来看,我无法满足我的要求。
无论我单击哪个按钮,我得到的部门 ID 输出都是 1.

有人可以帮助我吗?

无需使用隐藏输入,您只需使用 button 标签即可:

<?php while($row = mysqli_fetch_array($result)) { ?>
    <tr>
        <td align="center"><?php echo $row['dept_ID']; ?></td>
        <td align="center"><?php echo $row['dept_name']; ?></td>
        <td>
            <button type="submit" name="departmentID" class="buttonsPromptConfirmDeleteDepartment" value="<?php echo $row['dept_ID']; ?>">Delete</button>
        </td>
    </tr>
<?php } ?>

当然,在执行表单处理的 PHP 脚本中,像往常一样访问 POST 索引:

$id = $_POST['departmentID'];
// some processes next to it

注意:不要忘记<form>标签。

附加说明:不要忘记使用预准备语句

$sql = 'DELETE FROM department WHERE dept_ID = ?';
$stmt = $dbc->prepare($sql);
$stmt->bind_param('i', $id);
$stmt->execute();
// some idea, use error checking when necessary
// $dbc->error

更改

id="部门ID">

类="部门 ID" 和

改变

<script>
$(document).ready(function(){
   $(".buttonsPromptConfirmDeleteDepartment").click(function(){
    var departmentID = $('input#departmentID').val();
    alert(departmentID);
   });
});

 <script>
 $(document).ready(function(){
    $(".buttonsPromptConfirmDeleteDepartment").click(function(){
    var departmentID = $(this).next('input.departmentID').val();
    alert(departmentID);
 });
 });

>首先dept_id在while循环中,并且您对所有部门使用相同的id。另一件事,你可以得到dept_id 点击按钮后使用jQuery..喜欢这个

$('.buttonsPromptConfirmDeleteDepartment').click(function(){
dept_id = $(this).next('input').val();
})