Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将项目添加到购物车_Javascript_Php_Mysql - Fatal编程技术网

Javascript 将项目添加到购物车

Javascript 将项目添加到购物车,javascript,php,mysql,Javascript,Php,Mysql,我是php新手,请接受我的知识不足。 我正在开发一个简单的购物车。我将屏幕分为三个framset。左侧是一个imagemap,用户可以在其中单击所需的项目。topRight是一个php页面,用于检索并在表中显示在leftFrame中单击的项目的详细信息。此表的最后一个单元格有一个文本字段和一个按钮,用于提交订单。 bottomRightframe的目标是显示购物车,向用户显示已经选择了多少产品。 我的问题是,到目前为止,我只能显示当前提交的订单。换句话说,我只显示topRigt框架中最后一个“订

我是php新手,请接受我的知识不足。 我正在开发一个简单的购物车。我将屏幕分为三个
framset
。左侧是一个
imagemap
,用户可以在其中单击所需的项目。
topRight
是一个
php
页面,用于检索并在表中显示在
leftFrame
中单击的项目的详细信息。此表的最后一个单元格有一个
文本字段
和一个
按钮
,用于提交订单。
bottomRight
frame的目标是显示购物车,向用户显示已经选择了多少产品。 我的问题是,到目前为止,我只能显示当前提交的订单。换句话说,我只显示
topRigt
框架中最后一个“订购”项目的详细信息。 我如何继续添加第二项而不是替换它

这是左上角的代码

<html>
<head>
<title>Top Right</title>
<style type="text/css">
<!--
    @import url("style.css");
-->
</style>
</head>
<body bgcolor = "#E0E6F8">
    <h2>ITEMS</h2>
<?php
session_start();

// get the id from the left frame
$id = $_GET['var'];

// variable to connect to db
$user_name = "name";
$password = "password";
$server = "server";
$link = new mysqli($server,$user_name,$password,'poti');

if(!$link)
    die("Could not connect to databse");

$result = $link->query("SELECT * FROM products WHERE product_id = '$id';");

while($row = $result->fetch_array())
{
    $id = $row['product_id'];
    $name = $row['product_name'];
    $unit_price = $row['unit_price'];
    $unit_quantity = $row['unit_quantity'];
    $in_stock = $row['in_stock'];
    $arrayName = array('id' => $id, 'name' => $name);
    // store values in session
    $_SESSION['cart'] = $arrayName;

            // display details in a table (code needs to be refactored)
    if(!empty($row)){
        print "";
        print "<form method='post' name='add' id='add' action='' target=''>";
        print "<table  id='hor-minimalist-a' border = '0' width = '100%'>";
        print "<thead>
                    <tr>
                        <th scope='col'>ID</th>
                        <th scope='col'>Name</th>
                        <th scope='col'>Price</th>
                        <th scope='col'>QTY</th>
                        <th scope='col'>In Stock</th>
                        <th scope='col'>Order</th>
                    </tr>
                </thead>";
                print "<tr>\n";
                print "<td>";
                print($id);
                print "</td>";
                print "<td>";
                print($name);
                print "</td>";
                print "<td>";
                print($unit_price);
                print "</td>";
                print "<td>";
                print($unit_quantity);
                print "</td>";
                print "<td>";
                print($in_stock);
                print "</td>";

                    print "<td>
                                <input type='text' name='qty_ordered' size='4'><input type='submit' name='submit' value='Submit' onclick='checkQty()'>
                            </td>";
                print "</tr>";

                }
                print"</table>"; 
                print "</form>";
    }
?>


<script type="text/javascript">

function checkQty()
{
if(document.add.qty_ordered.value > 0 && document.add.qty_ordered.value <= 20)
{
    document.add.action = "bottomRight.php";
    document.add.target = "BottomRight";
    document.add.submit();
}
else
{
    //document.add.action = "topRight.php";
    //document.add.target = "TopRight";
    alert("Quantity must be between 1 and 20, please enter a value between 1 to 20"); 
    //document.add.submit();
}
}
</script>
</body>
</html>

查看youtube上Adam Khoury提供的视频教程。他使用了
mysql.*
(我知道它已经被弃用)函数,但你可以使用自己的代码解决问题。这段视频仅供参考

以下是youtube播放列表链接:

以下是解决您的问题的特定视频链接:


希望这对您有所帮助。

请不要使用框架集/框架;我知道,从90年代开始,这就不是一个好的做法,而且已经被官方否决了。但这就是我被要求的方式。。我不能改变它
<html>
<head>
<title>Bottom Right</title>
<style type="text/css">
<!--
    @import url("style.css");
-->
</style>
</head>
<body>
    <h2>Shopping Cart</h2>
<?php 
session_start();
    // doing this I keep on displaying only the last item "ordered"
if (isset($_POST['submit'])===true) {
      if(isset($_SESSION['cart'])===true) {
    $array = array($_SESSION['cart']);
    print_r($array);

     // not working
/*foreach($array as $a)
{
    echo "<table><tr>";
    echo "<td width='150px'><span style='color:white'>" . $a['id'] ."</span></td>";
    echo "</tr></table>";}*/
    }
}   
?>
</body>
</html>