javascript - 使用 php 或 javascript 通过按钮 onclick

标签 javascript php jquery onclick

使用 foreach 我显示了带有两个按钮的条目 - 分别更新和删除,如何使用 onclick(php 或 javascript)使这些按钮响应? Please click here to view attachment 。谢谢。

注意:未标记的按钮位于条目中 - 最右侧 代码如下:

$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']."'");
}

最佳答案

我创建了一个隐藏字段来保存 $product_code 变量的值,并去掉了 anchor 标记,因为我在 ajax 中处理了这个问题。 然后我在 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>';}

这部分是jquery与ajax部分

$(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);
            }
        },
    });
}
});

您可以执行所需的任务,移动到检查是否触发更新或删除操作的页面

    <?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']."'");
}

 ?> 

希望这有帮助 DELETE 语法已修复,您可以从中找到更多信息 dev.mysql.com/doc/refman/5.7/en/delete.html感谢@Fred

关于javascript - 使用 php 或 javascript 通过按钮 onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38928675/

相关文章:

php - 调用已扩展布局的 Blade 中的部分 View : Laravel 5. 2.37

javascript - 如何突出显示文本,然后保存它以便下次我访问同一页面时显示它?

javascript - 如何将相对路径设置为 "a href"?

JavaScript 正则表达式不起作用

Javascript/jQuery 字符串操作,将 <b> 标签包裹在所有文本的字符串周围,并在其后直接添加冒号

php - 使用一个查询插入多个数组

javascript - Bootstrap Collapse show.bs.collapse 第二次不工作

javascript - 如何清除asp :TextBox when user again open the pop up window?的文字

javascript - Onclick事件无法传入mysql

php - Mysql批量插入不插入所有数据