[英]onclick via button using php or javascript
Using foreach I displayed entries with two buttons along with it - update and delete respectively, how to make those buttons responsive using onclick(php or javascript)? 使用foreach我显示带有两个按钮的条目 - 分别更新和删除,如何使用onclick(php或javascript)使这些按钮响应? Please click here to view attachment . 请点击此处查看附件 。 Thanks. 谢谢。
note: the unlabelled buttons are along the entries - rightmost Here's the code: 注意:未标记的按钮位于条目旁边 - 最右边这是代码:
$get_product_details = $db->query("SELECT product_name,product_type FROM products WHERE product_code = '$product_code'");
foreach($get_product_details as $key) {
echo'<tr><td>'.$key['product_name'].'</td><td>'.$key['product_type'].'</td><td><input type = "text" name = "actual_case" value = "'.$quantity.'"class = "form-control" style = "width:20%;"></td>
<td><a href="?update='.$product_code.'"<input type = "button" name = "update" class = "form-control" value ="update" style = "width:20%;height:0"></a></td>
<td><a href="?remove='.$product_code.'"<input type = "button" name = "remove" id = "" class = "form-control" value ="remove" style = "width:20%;height:0"></a></td></td></tr></br>';
}
if(isset($_GET['remove'])){
$delete_entry = $db->query("DELETE * FROM order_supplier_list WHERE order_supplier_id = '$transaction_id' AND product_code = '".$_GET['remove']."'");
}
if(isset($_GET['update'])){
$qntity = $_POST['actual_case'];
$update_order = $db->query("UPDATE order_supplier_list SET quantity = '$qntity' WHERE order_supplier_id = '$transaction_id' AND product_code = '".$_GET['update']."'");
}
i created a hidden field to hold the value of the $product_code variable and took away the anchor tags since i took care of that in ajax. 我创建了一个隐藏字段来保存$ product_code变量的值并带走了锚标签,因为我在ajax中处理了这个问题。 i then get this value in jquery,build the url with it in the ajax section. 然后我在jquery中获取此值,在ajax部分中使用它构建url。
foreach($get_product_details as $key) {
echo'<tr><td>'.$key['product_name'].'</td> <td>'.$key['product_type'].'</td><td><input type = "text" name = "actual_case" value = "'.$quantity.'"class = "form-control" id="quantity" style = "width:20%;"></td>
<td><input type = "hidden" name = "product_code" class = "form-control" value ="'.$product_code.'" style = "width:20%;height:50"></td>
<td><input type = "button" name = "update" class = "form-control" value ="update" style = "width:20%;height:50" onclick="updateProduct()"> </td>
<td><input type = "button" name = "remove" id = "" class = "form-control" value ="remove" style = "width:20%;height:50" onclick="deleteProduct()></td></td></tr></br>';}
this part is the jquery with ajax part 这部分是带有ajax部分的jquery
$(function(){
function updateProduct(){
var hiddenProduct_Code=$('#hiddenProduct_code').val();
var quantity=$('#quantity').val();
$.ajax({
method: 'GET',
url: "?update="+hiddenProduct_Code+"",
data: {'update' : hiddenProduct_Code,'actual_case':quantity},
success: function (data) {
if (data) {
alert(data);
}
},
});
}
function deleteProduct(){
var hiddenProduct_Code=$('#hiddenProduct_code').val();
$.ajax({
method: 'GET',
url: "?remove="+hiddenProduct_Code+"",
data: {'remove' : hiddenProduct_Code},
success: function (data) {
if (data) {
alert(data);
}
},
});
}
});
you can perform the needed task move to the page where you check whether an update or delete action was triggered 您可以执行所需的任务移动到您检查是否已触发更新或删除操作的页面
<?php
if(isset($_GET['remove'])){
$delete_entry = $db->query("DELETE FROM order_supplier_list WHERE order_supplier_id = '$transaction_id' AND product_code = '".$_GET['remove']."'");
}
if(isset($_GET['update'])){
$qntity = $_GET['actual_case']; //I change it from $_POST to ensure uniformity
$update_order = $db->query("UPDATE order_supplier_list SET quantity = '$qntity' WHERE order_supplier_id = '$transaction_id' AND product_code = '".$_GET['update']."'");
}
?>
hope this helps DELETE syntax was fixed you can find out more from dev.mysql.com/doc/refman/5.7/en/delete.html thanks to @Fred 希望这有助于DELETE语法是固定的,你可以找到更多dev.mysql.com/doc/refman/5.7/en/delete.html感谢@Fred
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.