【发布时间】:2021-01-08 17:47:22
【问题描述】:
我有一个小页面,购物车类型,我确实想对代码进行一些更改。当我单击删除时,该项目将被删除,而无需重新加载页面。我试过了,但它不起作用。有人吗? 所以我更新了帖子,我把所有的 HTML 代码都放在这里了。
<?php
require_once "conectare.php";
if($_SESSION['admin_logat'] == false) {
header("Location: login.php");
}
$sql1 = "SELECT * FROM tbl_product ORDER BY id ASC";
$products = mysqli_query($con, $sql1);
?>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function trimite(e){
let xhr=new XMLHttpRequest();
xhr.onreadystatechange=function(r){
if( this.status==200 && this.readyState==4 ){
document.getElementById("RezervatAfisare").innerHTML=r
}
xhr.open('POST','product_delete.php',true);
xhr.send('id='+e.target.dataset.id);
}
function procesareRaspuns()
{
if ((cerere.readyState==4)&&(cerere.status==200))
{
raspuns=cerere.responseText
document.getElementById("RezervatAfisare").innerHTML=raspuns
}
}
</script>
<title>PRODUCTS</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<style type="text/css">
h1 {
text-align: center;
}
</style>
</head>
<body>
<nav class="navbar navbar-expand-md navbar-dark bg-dark">
<div class="navbar-collapse collapse w-100 order-1 order-md-0 dual-collapse2">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="index.php">HOME</a>
</li>
<li class="nav-item">
<a class="nav-link" href="gestionare_produse.php">PRODUCTS</a>
</li>
<li class="nav-item">
<a class="nav-link" href="gestionare_clienti.php">CLIENTS</a>
</li>
</ul>
</div>
<div class="navbar-collapse collapse w-100 order-3 dual-collapse2">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="logout.php"><button type="button" class="btn btn-outline-danger">LOGOUT</button></a>
</li>
</ul>
</div>
</nav>
<h1>MANAGE PRODUCTS</h1>
<br><br>
<a href="product_new.php"><button class="btn btn-primary">NEW PRODUCT</button></a><br><br>
<div>
<table class="table table-striped table-hover">
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Code</th>
<th>Image</th>
<th></th>
<th></th>
</tr>
<?php while($product = mysqli_fetch_assoc($products)) { ?>
<tr>
<td><?php echo $product['id']; ?></td>
<td><?php echo $product['name']; ?></td>
<td><?php echo strtok($product['description'], " ") . "..."; ?></td>
<td><?php echo $product['price']; ?></td>
<td><?php echo $product['code']; ?></td>
<td><?php echo "<img src=../images/".$product['image']." style='width:50px' alt=".$product['image'].">"; ?></td>
<td><a class="action" href="<?php echo "product_edit.php?id=" .$product['id']; ?>">Edit</a></td>
<td> <a class="action" data-id='<?php echo $product['id'];?>' onclick="trimite(this.value)">Delete</a></td>
</tr>
<?php } ?>
</table>
</div>
</body>
</html>
<?php
mysqli_close($con);
?>
and here is the product_delete.php
<?php
session_start();
error_reporting( E_ALL );
require_once "conectare.php";
if( empty( $_SESSION['admin_logat'] ) ) {
exit( header("Location: login.php") );
}
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['id'] ) ){
$id=$_POST['id'];
$sql='delete from `tbl_product` where `id`=? order by `id` limit 1';
$stmt=$con->prepare( $sql );
$stmt->bind_param( 'i', $id );
$stmt->execute();
$rows=$stmt->affected_rows;
$stmt->close();
exit( $rows ? 'OK' : 'FAILED' );
}
?>
这是我尝试做的脚本,但它不起作用
所以我更新了帖子,我把所有的 HTML 代码都放了
【问题讨论】:
-
当您说
"it doesn't work"时,您能澄清一下什么不起作用吗?项目/记录是否没有被删除或页面是否重新加载? -
我还建议对这种类型的 sql 命令使用
POST而不是GET并使用Prepared Statements,因为您的代码很容易受到 sql 注入攻击 -
因此,首先它起作用了,但正在重新加载页面。所以我尝试使用 javascript,因为我不想重新加载页面
-
那么javascript函数使用
this.value作为它的参数。按钮或其他 INPUT 元素将具有值,但超链接没有,因此您不会将 ID 传递给后端 php 脚本。由于您只显示了一个小 html,因此不清楚在哪里有具有该 ID 值的 HTML 元素。也许添加完整的 html 表单? -
我刚刚更新了。感谢您对 Abronsius 教授的关注
标签: javascript php html xml