当我按下带有类测试按钮的按钮时,我想重新加载一个div但是我的帖子在add_product.php中添加产品时无法识别。我不确定这是否是解决我问题的正确方法。任何帮助都非常感谢。我们的想法是,在添加产品时,页面不会仅刷新div。
我现在要做的是:
Homepage.php 正在加载我的所有产品:
$("#results").load("products.php", {'page':0}, function() {$("#1-page").addClass('active');});
在 products.php 中,此代码正在加载我的添加到购物车脚本:
$(".fillthisdiv").load("add_product.php");
我的 add_product.php 无法识别帖子,因此无法添加产品
$_POST['pid'];
添加的产品以
显示 if (isset($_SESSION['cart_array'])){
if ($_SESSION['cart_array'] != NULL){
echo $cart_output_bestellijst;
}
}
Homepage.php
session_start();
include "scripts/connect_to_mysql.php";
// This loads a list with all products and show it in #results div
$(document).ready(function($) {
$("#results").load("products.php", {'page':0}, function() {$("#1-page").addClass('active');});
});
// Displaying all products
<div id="results"></div>
// This Div should only show the added products
<div class="fillthisdiv"></div>
Products.php
session_start();
include("../connect_to_mysql.php"); //include config file
product_list = "";
while($row = mysql_fetch_array($results)){
$id = $row["id"];
$product_name = substr($row["product_name"],0,25);
$price = $row["price"];
$product_list .= "<form id='form1' name='form1' method='post' action=''>
<div class='product>
<span class='prijs-info'>Price:</span><span class='prijs'><label id='costLabel' name='costLabel'>$price</label> </span>
<span class='productnaam'>$product_name</span>
<input type='hidden' name='pid' id='pid' value='$id' />
<input type='submit' name='button' id='button' class='testbutton' value='' onclick='return false' />
</form>
</div>
";
}
echo $product_list;
// This script will fill the div class fillthisdiv with added products.
<script type="text/javascript">
$(document).ready(function($) {
$('.testbutton').click(function(){
$(".fillthisdiv").load("add_product.php");
});
});
</script>
add_product.php
session_start();
include("../connect_to_mysql.php"); //include config file
if (isset($_POST['pid'])) {
$pid = $_POST['pid'];
$wasFound = false;
$i = 0;
// If the cart session variable is not set or cart array is empty
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
// RUN IF THE CART IS EMPTY OR NOT SET
$_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
} else {
// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
foreach ($_SESSION["cart_array"] as $each_item) {
$i++;
while (list($key, $value) = each($each_item)) {
if ($key == "item_id" && $value == $pid) {
// That item is in cart already so let's adjust its quantity using array_splice()
array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
$wasFound = true;
} // close if condition
} // close while loop
} // close foreach loop
if ($wasFound == false) {
array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
}
}
}
$cartTotal = "";
if (isset($_SESSION['cart_array'])){
if ($_SESSION['cart_array'] != NULL){
$cart_output_bestellijst = '';
// Start the For Each loop
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$item_id = $each_item['item_id'];
$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
while ($row = mysql_fetch_array($sql)) {
$product_name = $row["product_name"];
$price = $row["price"];
}
$cart_output_bestellijst .= '$product_name . " " . $price';
$i++;
}
}
}
echo "<h3>The added products</h3>";
if (isset($_SESSION['cart_array'])){
if ($_SESSION['cart_array'] != NULL){
echo $cart_output_bestellijst;
}
}
答案 0 :(得分:3)
您不是通过表单发送变量,因此不存在$_POST
变量。
还有其他方法可以处理此方案,但在您的情况下更改
$(".fillthisdiv").load("add_product.php");
到
$(".fillthisdiv").load("add_product.php?id=$id");
并使用$_GET
来获取该值。