这是我要完成的工作流程。
在我的 Users.html 页面加载时,我执行 php 代码以从 mysql 数据库中获取数据,然后使用 php 创建一个动态表以循环并为每行创建表行和数据元素。
每行都有一个删除按钮;按下删除按钮后,我打开一个 jQuery UI 模态对话框,其中传入并显示在对话框中的用户数据。
如果用户点击确认删除用户,我使用 $.post 打开一个 php 页面并传递 php 页面的数据以执行用户删除。
成功选择(或错误)后,我使用标题“users.html?error=x”,其中 x 是错误号,因此用户返回到他们开始的原始 users.html 页面。
问题是 users.html 页面永远不会被刷新(并且页面在整个 modal-php 重定向过程中实际上从未改变。
我使用 (#divIDx).remove() 删除表中的条目,但由于它是动态表,当用户更改表上的页面或选择或选择允许每页更多或更少的条目时,用户即使它们已从数据库中删除,也会返回。由于用户点击确认删除按钮,我尝试调用 document.location.reload(),但我的安全人员听到了该调用并终止了当前会话。
因此,要将其分解为 HTML 锚点,请单击 -> jquery 对话框 -> 删除用户 php -> 回到相同的 users.html 但 users.html 从不刷新。
这是执行数据库查询的 users.html
<?php
require_once( '_/connections/login.php' );
$stmt = $mysqli_login->stmt_init();
//$businessid = $mysqli_login->real_escape_string($orgid);
$stmt = $mysqli_login->prepare("SELECT firstname, lastname, role, email, id, organizationid FROM login where organizationid = " . $mysqli_login->real_escape_string($orgid) );
//$stmt = $mysqli_login->prepare("SELECT firstname, lastname, role, email, id, organizationid FROM login where organizationid = ?");
if(!isset($stmt)){}else{
///////////////////////////////////////////////////////////
//TODO: find out why bind_params is not working here
///////////////////////////////////////////////////////////
//$stmt->bind_params('s', $businessid);
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
$stmt->bind_result($Ufirstname, $Ulastname, $Urole, $Uemail, $Uid, $Uorgid);
}
?>
然后我使用这段代码创建我的表
<?php if(!$stmt){echo $users_found = 0;} else {$users_found = $stmt->num_rows;}?>
<div class="widget">
<div class="whead">
<h6>Registered Dashboard Users:
<?php echo $users_found; ?>
<?php if(isset($_GET['del_error'])){
switch($_GET['del_error']) {
case '0':
echo "User Deleted";
break;
case '1':
echo "Error deleting user";
break;
case '2':
echo "Error deleting user";
break;
case '3':
echo "Error deleting user";
break;
default:
break;
}
}?>
</h6>
<div class="clear"></div>
</div>
<div id="dyn" class="hiddenpars">
<a class="tOptions" title="Options"><img src="images/icons/options" alt="" /></a>
<table cellpadding="0" cellspacing="0" border="0" class="dTable" id="dynamic">
<thead>
<tr>
<th>First Name<span class="sorting" style="display: block;"></span></th>
<th>Last Name</th>
<th>Role</th>
<th>Email</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php while ($stmt->fetch()) { ?>
<tr id="div<?php printf ("%s", $Uid); ?>">
<td><?php printf ("%s", $Ufirstname); ?></td>
<td><?php printf ("%s", $Ulastname); ?></td>
<td><?php printf ("%s", $Urole); ?></td>
<td><?php printf ("%s", $Uemail); ?></td>
<td class="tableActs">
<a class="usereditbutton tablectrl_small bDefault tipS" title="Edit"
user-dlg-divid="div<?php printf ("%s", $Uid); ?>"
user-dlg-fname="<?php printf ("%s", $Ufirstname); ?>"
user-dlg-lname="<?php printf ("%s", $Ulastname); ?>"
user-dlg-role="<?php printf ("%s", $Urole); ?>"
user-dlg-email="<?php printf ("%s", $Uemail); ?>"
user-dlg-id="<?php printf ("%s", $Uid); ?>"
user-dlg-orgid="<?php printf ("%s", $Uorgid); ?>"
><span class="iconb" data-icon=""></span></a>
<a class="userdeletebutton tablectrl_small bDefault tipS" title="Remove"
user-dlg-divid="div<?php printf ("%s", $Uid); ?>"
user-dlg-fname="<?php printf ("%s", $Ufirstname); ?>"
user-dlg-lname="<?php printf ("%s", $Ulastname); ?>"
user-dlg-role="<?php printf ("%s", $Urole); ?>"
user-dlg-email="<?php printf ("%s", $Uemail); ?>"
user-dlg-id="<?php printf ("%s", $Uid); ?>"
user-dlg-orgid="<?php printf ("%s", $Uorgid); ?>"
><span class="iconb" data-icon=""></span></a>
</td>
</tr>
<?php } $stmt->close(); ?>
</tbody>
</table>
</div>
</div>
</div>
表单 div 很简单,因为我从另一个 HTML 页面动态注入 HTML
<div id="dialog_edituser" title="Edit User Form">
<a id="dialog_edituser_content"></a>
</div>
对话框的 javscript 代码获取必要参数的值,将它们存储在一些全局变量中,然后在打开时,HTML 的内容被注入到适当的 div 中,服务器已经使用一些填充了所需的动态数据回显 $_POST['...'] 命令
//==================================//
//===== Remove User Dialog Box =====//
//==================================//
var dialog_deluser_divid = ''; //tr to delete from table
var dialog_deluser_fname = ''; //first name
var dialog_deluser_lname = ''; //last name
var dialog_deluser_email = ''; //email
var dialog_deluser_role = ''; //role
var dialog_deluser_id = ''; //id
var dialog_deluser_orgid = ''; //business id
$('#dialog_deluser').dialog({
autoOpen: false,
width: 400,
modal: true,
buttons: {
"Submit": function() {
//remove the data from the table
$('#'+dialog_deluser_divid).remove();
//remove the user from the database
$.post("_/processUserRemove.php", {
'fname': dialog_deluser_fname,
'lname': dialog_deluser_lname,
'email': dialog_deluser_email,
'role': dialog_deluser_role,
'id': dialog_deluser_id,
'orgid': dialog_deluser_orgid
});
//close the dialog box
$( this ).dialog( "close" );
},
"Cancel": function() {
$( this ).dialog( "close" );
}
},
//load the dialog box with the contents of an HTML file replacing the ID in a <div id=
open: function(event, ui) {
$('#dialog_deluser_content').load('_/processUserRemoveDialog.html',
{'divid':dialog_deluser_divid,
'fname':dialog_deluser_fname,
'lname':dialog_deluser_lname,
'email':dialog_deluser_email,
'role':dialog_deluser_role,
'id':dialog_deluser_id,
'orgid':dialog_deluser_orgid},
function() {
});
}
});
$('.userdeletebutton').click(function (e) {
e.preventDefault();
dialog_deluser_divid = $(this).attr("user-dlg-divid");//id of tr to delete
dialog_deluser_fname = $(this).attr("user-dlg-fname");//set first name
dialog_deluser_lname = $(this).attr("user-dlg-lname");//set last name
dialog_deluser_email = $(this).attr("user-dlg-email");//set email
dialog_deluser_role = $(this).attr("user-dlg-role");//set role
dialog_deluser_id = $(this).attr("user-dlg-id");//set id
dialog_deluser_orgid = $(this).attr("user-dlg-orgid");//set organization id;
$('#dialog_deluser').dialog('open');
});
//===== End Remove User Dialog Box =====//
一旦用户点击提交按钮,值就会发布到 processUserRemove.php 并且 processUserRemove.php 将执行 header('Location: ../users.html?del_error=x'); 其中“x”是相应的错误号。
这是 processUserRemove.php 代码
<?php
require_once( './connections/login.php' );
require_once( 'login_functions.php' );
sec_session_start(); // Our custom secure way of starting a php session.
if(isset($_POST['fname'], $_POST['lname'], $_POST['email'], $_POST['role'], $_POST['id'], $_POST['orgid'])) {
//////////////////////////////////////////////////////////////////////
//TODO: get the associated business ID from the current user
//////////////////////////////////////////////////////////////////////
$RUfirstname = $_POST['fname'];
$RUlastname = $_POST['lname'];
$RUemail = $_POST['email'];
$RUrole = $_POST['role'];
$RUid = $_POST['id'];
$RUorgid = $_POST['orgid'];
$RU_currentuser_orgid = $_SESSION['organizationid'];
$del_stmt = $mysqli_login->init();
if ($RU_currentuser_orgid == $RUorgid) {
if ($del_stmt = $mysqli_login->prepare("DELETE FROM login WHERE firstname = ? and lastname = ? and email = ? and role = ? and organizationid = ? and id = ?")) {
$del_stmt->bind_param('ssssss', $RUfirstname, $RUlastname, $RUemail, $RUrole, $RUorgid, $RUid);
// Execute the prepared query.
$del_stmt->execute();
header('Location: ../index.html');
} else {
// Registration Failed
header('Location: ../users.html?del_error=1');
}
} else {
// orgainization id's don't match
header("Location: ../users.html?del_error=2_firstname=" . $RUfirstname . "_lastname=" . $RUlastname .
"_email=" . $RUemail . "_role=" . $RUrole . "_organizationid=" . $RUorgid . "_id=" . $RUid . "_cuid=" . $RU_currentuser_orgid);
}
} else {
// The correct POST variables were not sent to this page.
header('Location: ../users.html?del_error=3');
}
?>
但是在button -> modal -> php -> users.html的整个过程中,users.html页面从不刷新。删除表格条目是可行的,但是当用户与表格交互时,删除的数据又回来了,并且 document.location.reload(true) 将我从系统中注销?!?
对不起所有的代码,但我想非常清楚我是如何遗漏了什么,或者我做了多么复杂的操作,这应该是一个简单的操作。
非常感谢任何帮助,谢谢。