Reputation: 795
Cont on PHP - pass hidden value into the jquery
<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(){
$("#dialogConfirmDeleteDepartmentBox").dialog({
modal: true,
resizable: false,
width: 300,
height: 150,
dialogClass: "dialogConfirmDeleteDialogBox",
position: { my: 'top', at: 'top+300' },
open: function() {
var message = 'Are you sure you want to delete this department?';
$(this).html(message);
},
buttons:
[
{
text: "OK",
click: function()
{
var departmentID = $(this).next('input.departmentID').val();
alert(departmentID);
},
style:"margin-right: 60px;"
},
{
text: "Cancel",
click: function ()
{
$(this).dialog("close");
},
style:"margin-left: 0px;"
},
]
});
});
});
</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>
<div id="dialogConfirmDeleteDepartmentBox" title="Confirm"></div>
<input type="hidden" class="departmentID" value="<?php echo $row['dept_ID']; ?>" />
</td>
</tr>
<?php
}
?>
</table>
<?php
}
?>
department table
dept_ID dept_name 1 Account 2 Finance 3 Marketing
This time I add a dialog box inside my code.
Assume that my department table only have 3 records.
My requirement is the following:
- Click 1st delete button, show dialog box, click OK, show department ID = 1
- Click 2nd delete button, show dialog box, click OK, show department ID = 2
- Click 3rd delete button, show dialog box, click OK, show department ID = 3
However, I get undefined value no matter what button I clicked.
Can someone help me?
Upvotes: 0
Views: 66
Reputation: 6615
$(this)
in
var departmentID = $(this).next('input.departmentID').val();
does not refer to your DOM object but to the current dialog.
Create a reference to your DOM object first, which you can then use later on:
$(document).ready(function(){
$(".buttonsPromptConfirmDeleteDepartment").click(function(){
var $box = $(this);
...
...
click: function()
{
var departmentID = $box.parent().find('input.departmentID').val();
alert(departmentID);
}
...
...
});
});
Upvotes: 2
Reputation: 67515
You could select the input by the class name departmentID
, use :
$(this).closest('td').find('.departmentID').val();
Instead of :
$(this).next('input.departmentID').val();
Hope this helps.
Upvotes: 0