Javascript 无法在其他.php页面上添加到购物车

Javascript 无法在其他.php页面上添加到购物车,javascript,php,html,e-commerce,Javascript,Php,Html,E Commerce,我正在尝试创建一个简单的电子商务网站。一切进展顺利,但我只能在index.php中添加产品。当我尝试单击all_products.php页面上的add_cart按钮时,它会将我重定向到index.php页面,并且不会将产品添加到购物车 我希望能够在all_products.php页面上添加产品。我的代码有什么问题: function.php(创建“添加到购物车”按钮的函数,以及单击“添加到购物车”按钮时将发生什么的函数: function getProd(){ if(!isset($_G

我正在尝试创建一个简单的电子商务网站。一切进展顺利,但我只能在
index.php
中添加产品。当我尝试单击
all_products.php
页面上的
add_cart
按钮时,它会将我重定向到
index.php
页面,并且不会将产品添加到购物车

我希望能够在
all_products.php
页面上添加产品。我的代码有什么问题:

function.php
(创建“添加到购物车”按钮的函数,以及单击“添加到购物车”按钮时将发生什么的函数:

function getProd(){
    if(!isset($_GET['cat'])){
    global $con;
    $get_prod = "select * from item order by RAND() LIMIT 0, 6";
    $run_prod = mysqli_query($con, $get_prod);
    while($row_prod = mysqli_fetch_array($run_prod)){
        $prod_id = $row_prod['ItemId'];
        $prod_name = $row_prod['ItemName'];
        $prod_desc = $row_prod['ItemDesc'];
        $prod_price = $row_prod['ItemPrice'];
        $prod_qty = $row_prod['ItemQty'];
        $prod_cat = $row_prod['ItemCat'];
        $prod_img = $row_prod['ItemImg'];

        echo "
            <div id='single_product'>

                <img src='../admin_int/product_images/$prod_img' width='180' height=180'/>
                <h4>Name:</h4><h4> $prod_name</h4>
                <p><b>Price: $ $prod_price</b></p>

                <a href='details.php?prod_id=$prod_id' style='float:left; font-size:19px;padding-top:10px;text-decoration:none'>Details</a>

                <a href='index.php?add_cart=$prod_id'><button style = 'float:right; border:1; border-radius:12px; color:blue;
                 height:50px; width:50px;
                 background-color: #80ff80'>
                Add to Cart</button></a> //THIS IS THE CREATION OF ADD TO CART BUTTON
            </div>
        ";
    }

}// if(!isset($_GET['cat'])){

}//END OF getProd()
all_products.php

     <!DOCTYPE html>
<?php 
include("../functions/function.php");
?>
<html>
    <head>
    <title>Online Shop</title>
    <link rel='stylesheet' href='../CSS/style.css' media="all"/>
    </head>
<body>  
            <!--Main Wrapper starts here!-->
    <div class="main_wrapper"> 

            <!--Header Wrapper starts here!-->
        <div class="header_wrapper" >
        <a href='index.php'><img id="logo" src="../Pictures/logo.png"/></a>
        </div>
            <!--Header ends here!-->

            <!--Menu Bar starts here!-->
        <div class="menubar">
        <ul id="menu">
            <li><a href="index.php"> Home </a></li> 
            <li><a href="all_products.php"> All Products </a></li>
            <li><a href="customer/my_account.php"> My Account </a></li>
            <li><a href=""> Sign Up </a></li>
            <li><a href="cart.php"> Shopping Cart </a></li>
            <li><a href="#"> Contact Us  </a></li>
        </ul>
        <div id="form">
            <form method="get" action="results.php" enctype="multipart/form-data">
                <input type="text" name="user_query"  placeholder="Search Product"/>
                <input type="submit" name="search" value="search" style='background-color:#80dfff; border:none;'/>
            </form> 
        </div>
        </div><!--Menubar ends here!-->

            <!--Content Wrapper here!-->
        <div class="content_wrapper">
            <div id="sidebar"> 
                <div id="sidebar_title"> Categories</div>
                <ul id="cats">

                    <?php getCats(); ?>     

                </ul>   
            </div>
            <!--CONTENT AREA starts here-->
            <div id="content_area">
                <?php cart(); ?>
                <div id='shopping_cart'>
                    <span style='float:right; font-size:18px; padding-right:5px; padding:5px; line-height:40px;'>
                     Welcome Guest!
                      <b style='color:#336600'>SHOPPING CART</b>
                      Total Items:<?php total_items(); ?>
                       Total Price: <?php total_price(); ?>
                     <a href="cart.php" style='color:#336600'>Go to Cart</a>

                    </span>

                </div>

                <div id="product_box">
                <!--CODE IN SHOWING ALL PRODUCTS-->
                <?php   
                    $get_prod = "select * from item order by ItemName";
                    $run_prod = mysqli_query($con, $get_prod);
                    while($row_prod = mysqli_fetch_array($run_prod)){
                        $prod_id = $row_prod['ItemId'];
                        $prod_name = $row_prod['ItemName'];
                        $prod_desc = $row_prod['ItemDesc'];
                        $prod_price = $row_prod['ItemPrice'];
                        $prod_qty = $row_prod['ItemQty'];
                        $prod_cat = $row_prod['ItemCat'];
                        $prod_img = $row_prod['ItemImg'];
                    echo "
                    <div id='single_product'>
                    <img src='../admin_int/product_images/$prod_img' width='180' height=180'/>
                    <h4>Name:</h4><h4> $prod_name</h4>
                    <p><b>Price: $ $prod_price</b></p>

                    <a href='details.php?prod_id=$prod_id' style='float:left; font-size:19px;padding-top:10px;text-decoration:none'>Details</a>

                    <a href='index.php?prod_id=$prod_id'><button style = 'float:right; border:1; border-radius:12px; color:blue;
                     height:50px; width:50px;
                     background-color: #80ff80'>
                    Add to Cart</button></a>
                    </div>
                    ";
                    }
                    ?> <!--END OF ALL PRODUCTS-->

                </div>

            </div><!--END OF CONTENT AREA-->
        </div><!--Content Wrapper ends here!-->
        <div id="footer"><h2 style='text-align:center; padding-top:25px;'>&copy; Jerlon Buenconsejo 2015</h2> </div>
    </div><!--Wrapper-->
</body>
</html>

网上商店
类别
欢迎光临! 购物车 项目总数: 总价:
您被重定向到index.php,因为这是链接中指定的页面:

<a href='index.php?prod_id=$prod_id'>

将其更改为页面名称。此外,还需要在页面顶部调用cart()函数

你应该考虑调用一个唯一的页面,它是AdcDAD.PHP的一个例子,它重定向到你插入产品后的页面,或者你可以用Ajax调用。

<a href='index.php?prod_id=$prod_id'>