我正在处理一个项目,我希望通过使用复选框中的值更新表来为用户分配角色。我想使用复选框更新多个记录。我想从下拉菜单中选择用户时更新新服务表。我现阶段需要帮助。
#assign estimator
$gp = $userDetails->pgroup;
$users = $db->prepare('SELECT
dbo.[GWCL$west_users].name AS fullname,
dbo.[GWCL$west_users].user_group AS pgroup,
dbo.[GWCL$west_users].username,
dbo.[GWCL$west_users].uid,
dbo.[GWCL$west_users].region,
dbo.[GWCL$west_users].district
FROM dbo.[GWCL$west_users]
WHERE
dbo.[GWCL$west_users].user_group = 10
');
$users->execute();
#update and assign estimating officer
if($_POST['process']){
foreach($_POST['dm_approve'] as $id)
{
$update =$db->prepare('UPDATE dbo.[GWCL$NewServiceConnection] SET dbo.[GWCL$NewServiceConnection].staffID = '.$_POST['staffID'].', dbo.[GWCL$NewServiceConnection].dm_approve = 1 WHERE dbo.[GWCL$NewServiceConnection].Application_ID ='.$id.'');
$update->execute();
$result = $update->rowCount();
if ($result ===false) {
echo "Failed";
}else{
echo "Status " .$id. " is assigned to ".$_POST['staffID'].". <br>";
}
}
}
?>
<?php include('header.php'); ?>
<body class="with-side-menu">
<header class="site-header">
<?php require_once('topnav.php');?>
<!--.container-fluid-->
</header><!--.site-header-->
<div class="mobile-menu-left-overlay"></div>
<?php require_once('nav.php');?>
<!--.side-menu-->
<div class="page-content">
<div class="container-fluid">
<header class="section-header">
<div class="tbl">
<div class="tbl-row">
<div class="tbl-cell">
<h2>NSC Applications Pending Estimation </h2>
<div class="subtitle"><?php #echo $aName['Name'];?></div>
</div>
</div>
</div>
</header><?php if(isset($_GET['msg']))
{
$Message = "<div class='alert alert-success alert-dismissible' role='alert'>
<button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>×</span></button>
<strong>Successful!</strong> Estimation is made on Applicant's account
</div>";
echo $Message;
}?>
<section class="card">
<div class="card-block">
<div class="col-sm-4"><form name="nmc" id="wrapped" method="POST" action="assignedcust.php">
</div> <div class="col-sm-4"><select name="staffID" autofocus class="form-control" id="staffID" >
<option value="" selected>Select Estimator</option>
<?php foreach($users as $user){?>
<option value="<?php echo $user['username'];?>" ><?php echo $user['fullname'];?></option>
<?php }?>
</select></div> <input type="submit" name="process" id="submit" value="Assign" class="btn btn-primary"/>
<table id="example" class="display table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>
<input type="checkbox" class="chk_boxes" label="check all" align="left"/> Select All
</th>
<th>Applicant Name </th>
<th>Activity</th>
<th>Mobile</th>
<th>Service Type</th>
<th>Purpose</th>
<th>House Number</th>
<th>Estimate</th>
</tr>
</thead>
<tbody>
<?php foreach ($asscust as $acust){?><tr>
<td>
<input type="hidden" name="dm_approve[]" id="dm_approve[]" value="<?php echo $acust['Application_ID']; ?>">
<input type="checkbox" name="dm_approve[]" id="dm_approve[]" value="<?php echo $acust['Application_ID']; ?>" class="chk_boxes1"><?php echo $acust['Application_ID']; ?>
</td>
<td><?php echo $acust['Applicant_Name'];?></td>
<td><?php echo $acust['Activity'];?></td>
<td><?php echo $acust['Mobile_No'];?></td>
<td><?php echo $acust['Service_Category'];?></td>
<td><?php echo $acust['Purpos_Supply'];?></td>
<td><?php echo $acust['House_No'];?></td>
<td><a class="btn btn-info" href="estimater.php?ID=<?php echo $acust['Application_ID'];?>"><i class="glyphicon glyphicon-pencil"></i> Estimate</a> | <a class="btn btn-default" href="assignesti.php?ID=<?php echo $acust['Application_ID'];?>"><i class="glyphicon glyphicon-user"></i> Assign</a> </td>
</tr><?php }?>
</tbody>
</table></form>
</div>
答案 0 :(得分:0)
这就是我所做的,如果你需要我解释它,只需发表评论。它只是在按下btn时检查所有检查的输入,然后它发出ajax请求并更新数据库中的值。我这样做了所以如果你检查了第四个输入,它将不会再次更新它,直到你刷新页面。
`<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script>
var arr=[];
function send()
{
var neww=[];
//get all of the inputs that are checked
$("input[type=checkbox]").each(function (input){
if ($(this).is(":checked")==true)
{
if (arr.indexOf($(this).attr('value'))<0)
{
arr.push($(this).attr('value'));
neww.push($(this).attr('value'));
}
}
})
var string = JSON.stringify(neww);
$.ajax({
type:"GET",
url:"up.php",
dataType:"json",
data:{"values": string},
success: function (res)
{
//do what u want when this complete
}
})
}
</script>
</head>
<body>
<input type="checkbox" id="check1" value="1">1</input><br>
<input type="checkbox" id="check2" value="2">2</input><br>
<input type="checkbox" id="check3" value="3">3</input><br>
<input type="checkbox" id="check4" value="4">4</input><br>
<input type="button" onclick="send()" value="Click me"></input>
</body>
</html>
`
up.php在这里:
<?php
$yes = array();
$string=json_decode($_GET["values"]);
//db connect
$con = new mysqli("localhost","yourusername","yourpass","yourdb");
foreach ($string as $val) {
$sql="INSERT INTO yourtabel Values ('',{$val})";
$con->query($sql);
}
//echo json_encode("bv");
?>