gpt4 book ai didi

php - 我怎样才能不刷新PHP代码中的页面

转载 作者:行者123 更新时间:2023-11-29 03:19:24 25 4
gpt4 key购买 nike

我正在构建一个购物车。我基本上为每个产品使用一个 item_id 来保存每个购物车的值。因为我正在调用新的 index.php,所以每当我从购物车中添加/删除项目时,它总是移动到页面顶部。

我试图将滚动位置保存为变量并将屏幕移动到之前的 scrollPos,但它不起作用....(既不调用 #names)。

有人知道如何不刷新页面来解决这个问题吗?

这是我的代码

<?php

session_start();

$page = 'index.php';
$con = mysqli_connect('localhost','root','0801','mystore') ;
mysqli_select_db($con,'mystore') ;

#for adding, removing, deleting the products from the cart
if(isset($_GET["add"])){ //same name with cart.php?add <--
$_SESSION['cart_'.(int)$_GET["add"]] += 1;
echo '<script>window.location="index.php"; window.scrollTo(0,1200);</script>';
}

if(isset($_GET['remove'])){
$_SESSION['cart_'.(int)$_GET['remove']] -- ;
header("Location: ".$page);
}

if(isset($_GET['delete'])){
$_SESSION['cart_'.(int)$_GET['delete']] = 0 ;
header('Location: '.$page);
}

#function for displaying products
function product(){
global $con;
$get = mysqli_query($con, 'SELECT * FROM inventory ORDER BY id ASC');

if (mysqli_num_rows($get) == 0){
echo("There are no products to display");
} else {
while ($get_row = mysqli_fetch_assoc($get)){
echo '<br /><img src="images/products/'.$get_row['image'].'" width=100px;>';
echo '<br>'.$get_row['name'].'<br /> &dollar;'.number_format($get_row['price'],2);

echo '<br> <a href="cart.php?add='.$get_row['id'].'">Add To Cart</a>
}
}
} #end of function


# function for display cart
function cart(){
foreach ($_SESSION as $key => $value) {
if ($value > 0){
if(substr($key, 0 , 5) =='cart_'){
global $con; //DON'T FORGET TO ADD THIS!!!
$id = substr($key, 5, (strlen($key)-5)); //take out the string part
$partid = mysqli_real_escape_string($con, $id);
$get = mysqli_query($con,'SELECT * FROM inventory WHERE id='.(int)$partid);

while($get_row = mysqli_fetch_assoc($get)){
$subTotal = $get_row['price'] * $value;
echo '<br /><img src="images/products/'.$get_row['image'].'" width=30px;>';
echo $get_row['name'].' x '.$value.' @ &dollar;'.number_format($get_row['price'], 2);
echo ' = &dollar;'.number_format($subTotal,2);
echo '<a href="cart.php?remove='.$id.'"> [-] </a> <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?delete='.$id.'"> [delete] </a><br />';
}
}
$total += $subTotal;
}
}
if ($total ==0 ){
echo "Your cart is empty";
} else {
echo '<p /> TOTAL: &dollar;'.number_format($total, 2);
}
}

print_r($_SESSION);

?>

最佳答案

考虑使用 jQuery Ajax调用以从购物车中添加/删除项目。这样做不需要重新加载页面。

关于php - 我怎样才能不刷新PHP代码中的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47785495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com