PHP - 将隐藏值传递给jquery [英] PHP - pass hidden value into the jquery

查看:59
本文介绍了PHP - 将隐藏值传递给jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < HTML> 
< head>
< link rel =stylesheethref =js / jquery-ui-themes-1.11.1 / themes / smoothness / jquery-ui.css/>
< script type =text / javascriptsrc =js / jquery-1.11.1.js>< / script>
< script type =text / javascriptsrc =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连接

$ 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 =600border =1cellpadding =0cellspacing =0style =border-collapse:collapse>
< tr>
< td width =80align =center> ID< / td>
< td width =300align =center>部门< / td>
< td width =220align =center>动作< / 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>删除< / button>
< input type =hiddenid =departmentIDvalue =<?php echo $ row ['dept_ID'];?> />
< / td>
< / tr>
<?php
}
?>
< / table>
<?php
}
?>

部门表格

dept_ID       ;  dept_name

1              Account

2               ;财务

3         nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;营销


假设我的部门表只有3条记录。

我的要求如下:
- 点击第一个删除按钮,显示部门ID = 1

- 点击第二个删除按钮,显示部门ID = 2

- 点击第三个删除按钮,显示部门ID = 3


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


有人可以帮我吗?

解决方案

不需要使用隐藏输入,只需使用按钮标记:

 <?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 =submitname =departmentIDclass =buttonsPromptConfirmDeleteDepartmentvalue =<?php echo $ row ['dept_ID'];?>>删除< / button>
< / td>
< / tr>
<?php}?>

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

  $ id = $ _POST ['departmentID']; 
//在它旁边的一些进程

注意:不要忘记标记。



其他注意:不要忘记使用准备好的语句

  $ sql ='删除部门WHERE dept_ID =?'; 
$ stmt = $ dbc-> prepare($ sql);
$ stmt-> bind_param('i',$ id);
$ stmt-> execute();
//一些想法,必要时使用错误检查
// $ dbc->错误


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

department table
dept_ID       dept_name
1                    Account
2                    Finance
3                    Marketing

Assume that my department table only have 3 records.
My requirement is the following:
- Click 1st delete button, show department ID = 1
- Click 2nd delete button, show department ID = 2
- Click 3rd delete button, show department ID = 3

However from my code, I can't meet my requirement.
The department ID output that I get is 1 no matter what button I clicked.

Can someone help me?

解决方案

No need to use a hidden input, you could just use the button tag instead:

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

Of course, in the PHP script that does the form processing, access the POST index like you normally would:

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

Note: Don't forget the <form> tag.

Additional Note: Don't forget to use prepared statements:

$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

这篇关于PHP - 将隐藏值传递给jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆