【发布时间】:2020-04-29 04:14:08
【问题描述】:
所以我使用'while'在我的主页上显示产品,我希望当用户单击产品磁贴时,它会被重定向到包含有关该特定产品的所有详细信息的文件 product_page.php 我不知道是不是因为我使用了“a”标签,但它不起作用
<?php
include 'config.php';
$stmt = $conn->prepare("SELECT * FROM product");
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()):
?>
<div class="col-sm-6 col-md-4 col-lg-3 mb-2" >
<a href="product_page.php">
<div class="card-deck" id="product">
<div class="card border border-light ">
<img class="card-img-top" src="<?= $row['product_image'] ?>" >
<div class="card-body border border-light" style="background-color: #ffffff">
<h4 class="card-title text-center text-info"><?= $row['product_name'] ?></h4>
<h5 class="card-text text-center text-danger">Rs. <?= number_format($row['product_price'],2) ?>
</h5>
</div>
<div class="card-footer border border-light" style="background-color: #ffffff">
<form action="" class="form-submit">
<input type="hidden" class="pid" value="<?= $row['id']?>">
<input type="hidden" class="pname" value="<?= $row['product_name']?>">
<input type="hidden" class="pprice" value="<?= $row['product_price']?>">
<input type="hidden" class="pimage" value="<?= $row['product_image']?>">
<input type="hidden" class="pcode" value="<?= $row['product_code']?>">
<button class="btn btn-block addItemBtn" style="background-color: #FE701E; color:#ffffff">Add To
Cart</button>
</form>
</div>
</div>
</div>
</a>
</div>
<?php endwhile; ?>
这是我的 action.php 代码
if(isset($_POST['pid']))
{
$pid = $_POST['pid'];
$pname = $_POST['pname'];
$pprice = $_POST['pprice'];
$pimage = $_POST['pimage'];
$stmt = $conn->prepare("SELECT * FROM product WHERE pid=? LIMIT 1");
$stmt->bind_param("i",$pid);
$stmt->execute();
$pcount=mysqli_num_rows($stmt);
$res = $stmt->get_result();
$r = $res->fetch_assoc();
if($pcode>0)
{
$_SESSION['product_name'] =$r['product_name'];
$_SESSION['product_price'] =$r['product_price'];
$_SESSION['product_image'] =$r['product_image'];
header("Location:product_page.php");
}
这里是ajax请求
$(document).ready(function(){
$("#product").click(function(e){
e.preventDefault();
var $form =$(this).closest(".form-submit");
var pid = $form.find(".pid").val();
var pname = $form.find(".pname").val();
var pprice = $form.find(".pprice").val();
var pimage = $form.find(".pimage").val();
var pcode = $form.find(".pcode").val();
$.ajax({
url:'action.php',
method:'post',
data:{pid:pid,pname:pname,pprice:pprice,pimage:pimage,pcode:pcode},
success:function(response){
}
});
});});
【问题讨论】:
-
回复
#product- 这可能会有所帮助 does id have to be unique -
我使用的id是唯一的
-
您有多个问题:1)
#product用于循环,这使得它重复且不唯一。最好使用class="product" data-id="<?= $row['id'] ?>"2) 非破坏性(读取请求)只能是 GET。无需表格或重定向。只需在 Get 请求中发送 ID。 3)您的ajax(如果您选择使用它)对成功没有任何作用。您更改位置的标头仅意味着它将 product_page.php 返回到调用 ajax 函数。据我所知,您甚至不想要这里的 ajax 功能。你希望它重定向。