javascript - 如何在不重新加载页面的情况下填充div

标签 javascript php jquery html ajax

当我按下带有类 testbutton 的按钮时,我想重新加载 div,但我的帖子无法识别在 add_product.php 中添加产品。我不确定这是否是解决我的问题的正确方法。非常感谢任何帮助。这个想法是,当添加产品时,页面不会只刷新 div。

我现在想做的事情:

  1. Homepage.php 正在通过以下方式加载我的所有产品:

    $("#results").load("products.php", {'page':0}, function() {$("#1-page").addClass('active');}); 
    
  2. products.php中,它通过以下代码加载我的添加到购物车脚本:

    $(".fillthisdiv").load("add_product.php");
    
  3. 我的add_product.php无法识别该帖子,因此无法添加产品

    $_POST['pid'];
    
  4. 添加的产品显示为

     if (isset($_SESSION['cart_array'])){
              if ($_SESSION['cart_array'] != NULL){
                  echo $cart_output_bestellijst;
              }
     }
    

主页.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>

产品.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;
              }
            }

最佳答案

您没有通过表单发送变量,因此不存在 $_POST 变量。 还有其他方法可以处理这种情况,但根据您的情况进行更改

$(".fillthisdiv").load("add_product.php");

$(".fillthisdiv").load("add_product.php?id=$id");

并使用$_GET获取该值。

关于javascript - 如何在不重新加载页面的情况下填充div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28408102/

相关文章:

javascript - 如何在提交后关闭彩盒窗口并刷新父页面上的图像?

javascript - Await 返回 [Function] 而不是值

php - 将上传文件移动到 IIS 中的另一台服务器的权限被拒绝

javascript - 在 CSS 中跟踪 Onclick 事件 - 执行一些操作

Javascript insideHTML 与其他页面的 session PHP

javascript - 如何在 Nuxt 中创建 v-model 自定义组件

javascript - 如何在 Map.get() 中的键上使用字符串方法

javascript - 先渲染 Javascript,再渲染 PHP

php - 拉维尔 8 : Class 'Database\Seeders\DB' not found

javascript - 验证不同页面的表单