我的系统可以使用复选框更新多个数据。这是我使用的代码。
updateproduct.php包含以下代码。
<?php
if (count($_POST["selected_id"]) > 0 ) {
$db = mysqli_connect('localhost', 'root', '', 'shoopingcart');
$all = implode(",", $_POST["selected_id"]);
$availability=$_POST['availability'];
$query="UPDATE internet_shop SET availability = '$availability' WHERE 1 AND id IN($all)";
if(mysqli_query($db,$query)){
$_SESSION['success'] = 'Products have been deleted successfully.';
}
}else{
$_SESSION['error'] = 'Select checkbox to delete product.';
}
header("Location:testproduct.php");
?>
现在还有一个选项可以删除选中的项目。我尝试在delete.php上使用此代码
<?php
if (count($_POST["selected_id"]) > 0 ) {
$db = mysqli_connect('localhost', 'root', '', 'shoopingcart');
$all = implode(",", $_POST["selected_id"]);
$query="DELETE FROM internet_shop WHERE 1 AND id IN($all)";
if(mysqli_query($db,$query)){
$_SESSION['success'] = 'Products have been deleted successfully.';
}
}else{
$_SESSION['error'] = 'Select checkbox to delete product.';
}
header("Location:testproduct.php");
?>
由于$ _POST [“ selected_id”]仅采用一种形式,因此该delete.php无法正常工作,并且该形式的操作重定向到updateproduct.php。
如何删除/更新多个数据?谢谢!下面是html代码
<form name="actionForm" action="updateproduct.php" method="post" onsubmit="return updateAlert();" id="updateproduct" />
<table cellpadding="1" cellspacing="1" id="resultTable">
<thead>
<tr>
<th style="text-align: center;"><input type="checkbox" name="check_all" id="check_all" value=""/></th>
<th class="sortable">Item</th>
<th class="sortable">Price</th>
<th class="sortable">Category</th>
<th style="text-align: center;">Action</th>
</tr>
</thead>
<?php
if(mysqli_num_rows($query) > 0){
while($row = mysqli_fetch_assoc($query)){
extract($row);
?>
<tr>
<td align="center"><input type="checkbox" name="selected_id[]" class="checkbox" value="<?php echo $id; ?>"/></td>
<td class="record"><?php echo $name; ?></td>
<td>₱ <?php echo $price; ?></td>
<td><?php echo $category; ?></td>
<td style="text-align: center;"><a rel="facebox" href="editproductetails.php?id='.$row['id'].'">Edit info</a> | <a href="#" id="'.$row['id'].'" class="delbutton" title="Click To Delete">delete</a></td>
</tr>
<?php } }else{ ?>
<tr><td colspan="3">No records found.</td></tr>
<?php } ?>
</table>
<select name="availability">
<option value="Test1"> 1 </option>
<option value="Test2"> 2 </option>
</select>
<input type="submit" class="btn btn-primary" name="btn_delete" value="Update Records" />
</form>
答案 0 :(得分:1)
您可以在使用javascript / jQuery提交之前更新表单操作,为此,您还需要删除“提交输入”按钮并创建多个按钮,每个操作一个,例如:
<table cellpadding="1" cellspacing="1" id="resultTable">
...
<button id="btn_update">Update</button>
<button id="btn_delete">Delete</button>
</table>
<form name="actionForm" method="post"></form>
使用以下脚本更新表单并提交
<script type="text/javascript">
function setFormActionAndSubmit(action) {
const checked = $("#resultTable input[name='selected_id[]']:checked");
if (!checked.length) {
alert("Please select an item.");
return;
}
// set form action
const form = $(document.actionForm).attr("action", action);
checked.each(function(){
$("<input/>", {type: "hidden", name: "selected_id[]", value: $(this).val()}).appendTo(form);
});
form.submit();
}
$("#btn_update").click(function() {
setFormActionAndSubmit("updateproduct.php")
});
$("#btn_delete").click(function(){
setFormActionAndSubmit("delete.php")
});
</script>
UPDATE-1 您还可以为每个操作创建单独的表单,它将允许您添加可用于特定表单的任何其他表单输入元素,例如:
// Update Form
<form name="updateForm" method="post" action="updateproduct.php">
<select name="availability">
<option value="Test1">1</option>
<option value="Test2">2</option>
</select>
</form>
// Delete Form
<form name="deleteForm" method="post" action="delete.php"></form>
使用以下脚本提交每个表单
<script type="text/javascript">
function setFormValuesAndSubmit(form) {
const checked = $("#resultTable input[name='selected_id[]']:checked");
if (!checked.length) {
alert("Please select an item.");
return;
}
form = $(form);
form.find("[name='selected_id[]']").remove();
checked.each(function(){
$("<input/>", {type: "hidden", name: "selected_id[]", value: $(this).val()}).appendTo(form);
});
form.submit();
}
$("#btn_update").click(function() {
setFormValuesAndSubmit(document.updateForm);
});
$("#btn_delete").click(function(){
setFormValuesAndSubmit(document.deleteForm);
});
</script>
UPDATE-2 以使用AJAX发送表单,将setFormCheckboxValuesAndSubmit
替换为以下内容:
function setFormCheckboxValuesAndSubmit(form) {
const checked = $("#resultTable input[name='selected_id[]']:checked");
if (!checked.length) {
alert("Please select an item.");
return;
}
form = $(form);
form.find("[name='selected_id[]']").remove();
checked.each(function(){
$("<input/>", {type: "hidden", name: "selected_id[]", value: $(this).val()}).appendTo(form);
});
$.ajax({
type: "POST",
url: form.attr('action'),
data: form.serialize(),
dataType: "json", // UNCOMMENT THIS IF RESPONSE IS JSON Object
success: function(responseData) {
alert("Form submitted successfully");
console.log(responseData);
},
error: function(x) {
alert("Something went worng")
console.log(x, x.responseText);
}
});
}
答案 1 :(得分:0)
您可以通过在提交表单的过程中传递操作来完成此操作,例如,单击更新和删除按钮时您具有更新和删除按钮,则传递操作类型=更新或删除。通过获取或发布并检查php文件中的操作。
if($ actionType ===“ update”)doupdate();如果($ actionType === “ delete”)dodelete();