Php Can´;t在表单提交后显示消息,并带有标题(“位置:”)
我在论坛上搜索了与我相同的情况,但仍然找不到解决方案。这可能是小菜一碟,但我不明白为什么我的$u GET[]不起作用 我已经创建了一个产品页面,当我向购物车添加一些东西时,我想显示一条消息。我已经使它与表单操作中的url一起工作,但是我的购物车计数器在标题中停止正常工作。 如果可能的话,我不想在url中添加任何额外的“?success”,因为如果我在购物车中添加更多内容,它只会继续向url添加?success,这会起作用,但不会与header()一起起作用 以下是我的产品页面代码:Php Can´;t在表单提交后显示消息,并带有标题(“位置:”),php,Php,我在论坛上搜索了与我相同的情况,但仍然找不到解决方案。这可能是小菜一碟,但我不明白为什么我的$u GET[]不起作用 我已经创建了一个产品页面,当我向购物车添加一些东西时,我想显示一条消息。我已经使它与表单操作中的url一起工作,但是我的购物车计数器在标题中停止正常工作。 如果可能的话,我不想在url中添加任何额外的“?success”,因为如果我在购物车中添加更多内容,它只会继续向url添加?success,这会起作用,但不会与header()一起起作用 以下是我的产品页面代码: <?p
<?php include_once '../header.php';
$message = "";
$product = New Product;
$cart_data = [];
// if the variables are set - run the following statement
if(isset($_POST["addtocart"])) {
if(isset($_COOKIE["cart"])) {
// Removes backlashes and dont replace previous item, gives every item a new row.
$cookie_data = stripslashes($_COOKIE['cart']);
$cart_data = json_decode($cookie_data, true);
}
// Returns the productid and Size in the array
$item_list = array_column($cart_data, 'ProductsId');
$size_list = array_column($cart_data, 'Size');
// Returns the value if the statement is true
if(in_array($_POST["ProductsId"], $item_list) && in_array($_POST['selectedSize'], $size_list)) {
// A foreachloop that repeats the array value of the selected key variable.
foreach($cart_data as $keys => $values) {
if($cart_data[$keys]["ProductsId"] == $_POST["ProductsId"] && $cart_data[$keys]["Size"] == $_POST["selectedSize"]) {
$cart_data[$keys]["quantity"] = $cart_data[$keys]["quantity"] + $_POST["quantity"];
}
}
}
else {
$item_array = array(
'Img' => $Img = filter_var($_POST["Img"], FILTER_SANITIZE_STRING),
'ProductName' => $ProductName = filter_var($_POST["ProductName"], FILTER_SANITIZE_STRING),
'Size' => $Size = filter_var($_POST['selectedSize'], FILTER_SANITIZE_STRING),
'ProductsId' => $ProductsId = filter_var($_POST["ProductsId"], FILTER_SANITIZE_NUMBER_INT),
'Price' => $Price = filter_var($_POST["Price"], FILTER_SANITIZE_NUMBER_INT),
'quantity' => $quantity = filter_var($_POST["quantity"], FILTER_SANITIZE_NUMBER_INT),
);
$cart_data[] = $item_array;
}
$item_data = json_encode($cart_data);
setcookie('cart', $item_data, time() +(3600),'/');
header("location: product-detail.php?product=".$_GET['product']."?success");
}
if(isset($_GET['success'])) {
$message = "Varan lades till i varukorgen";
};
var_dump($message);
?>
<main id="product-content">
<section>
<form method="post" name="cartCount" action="">
<!-- product-detail.php?product=<?php echo $_GET['product']; ?> -->
<?php if(isset($_GET['product'])) {
$product->ProductsId = $_GET['product'];
$product->ProductId = $_GET['product'];
$product->ProductsId = $_GET['product'];
} else {
$product->ProductsId = $_POST['ProductsId'];
}
$result = $product->get_product();
$test = $product->get_productvariation();
while ($row = $result->fetch()) { ?>
<div class="product-card-detail">
<div class="product-image-wrapper">
<img class="product-image" src="../<?php echo $row['Img'];?>" >
<input type ="hidden" name="Img" value="<?php echo $row['Img'] ?>">
<?php $results = $product->get_images();
$Images = $results->fetch();
if(isset($Images['Image'])) { ?>
<img class="product-image" src="../<?php echo $Images['Image'];?>">
<?php } ?>
</div>
<div class="product-details-text">
<h2 class="title"><?php echo $row['ProductName']; ?></h2>
<input type ="hidden" name="ProductName" value="<?php echo $row['ProductName'] ?>">
<span class="price"><?php echo $row['Price'];?> SEK</span>
<input type ="hidden" name="Price" value="<?php echo $row['Price'] ?>">
<span class="select-title">Storlek</span>
<select class="size" name="selectedSize">
<?php while ($sizeRow = $test->fetch()) { ?>
<option>
<?php echo $sizeRow['Size']; ?>
</option>
<?php } ?>
</select>
<input type="hidden" name="quantity" value="1" />
<input type="submit" class="addtocart-btn" name="addtocart" value="Lägg i varukorgen"/>
<div><?php echo $message ?></div>
<input type ="hidden" name="ProductsId" value="<?php echo $row['ProductsId'] ?>">
<span class="title-description">Beskrivning</span>
<p class="description"><?php echo $row['Description']; ?></p>
</div>
</div>
<?php } ?>
</form>
</section>
</main>
<?php include_once "../footer.php";?>
" >
你犯了一个错误:
header("location: product-detail.php?product=".$_GET['product']."?success");
请参见上行并注意,您正在将paramsuccess
添加到?
。
将其设置为:
header("location: product-detail.php?product=".$_GET['product']."&success");
你犯了一个错误:
header("location: product-detail.php?product=".$_GET['product']."?success");
请参见上行并注意,您正在将paramsuccess
添加到?
。
将其设置为:
header("location: product-detail.php?product=".$_GET['product']."&success");
所以,请澄清-您的代码按预期工作,您唯一的问题是不需要的文本被添加到URL?请澄清-您的代码按预期工作,您唯一的问题是不需要的文本被添加到URL?天哪,太简单了!!!!现在我觉得太愚蠢了!它当然工作了!它还解决了添加的问题更多?url成功:如果您遇到任何问题,请在此处询问。我们是来帮助您的。:)天哪,太简单了!!!!现在我觉得自己太愚蠢了!它当然有效!它还通过向url添加更多内容解决了问题?成功:如果您遇到任何问题,请在此处询问。我们是来帮助您的。:)