购物车,加入购物车按钮功能

Posted

技术标签:

【中文标题】购物车,加入购物车按钮功能【英文标题】:shopping cart, add to cart button function 【发布时间】:2014-04-03 14:38:43 【问题描述】:

脚本从数据库中读取产品并将项目放入表中。还包括每个项目旁边的复选框并添加到购物车按钮。

当用户选中一个或多个框并按下“添加到购物车”时,我希望该项目显示在“handlebasket.php”上

我该怎么做?

<?php
session_start();
?>
<?php
include 'header2.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Home</title>
<link rel="stylesheet" type="text/css" href="css/login.css">
<link rel="stylesheet" type="text/css" href="css/default.css">
</head>
<body>

 session_start();

$dbhost = 'igor.';
$dbuser = 's';
$dbpass = 's';
$rec_limit = 10;

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$conn) 
die('Could not connect: ' . mysql_error());

    mysql_select_db('ma302ha_coursework_network');
    /* Get total number of records */
    $sql = "SELECT count(ID) FROM Dvd_List";
    $retval = mysql_query($sql, $conn);
    if (!$retval) 
        die('Could not get data: ' . mysql_error());
    
            $row = mysql_fetch_array($retval, MYSQL_NUM);
            $rec_count = $row[0];

            if (isset($_GET'page')) 
                $page = $_GET'page' + 1;
                $offset = $rec_limit * $page;
             else 
                $page = 0;
                $offset = 0;
            
            $left_rec = $rec_count - ($page * $rec_limit);

            ?>
    <form action='loginhandler.php' method='post'>
            <input type="checkbox" name="year" value="BETWEEN 2010 AND   2014">2010 and Later
            <input type="checkbox" name="year" value="BETWEEN 2000 AND 2009">2000 - 2009
            <input type="checkbox" name="year" value="BETWEEN 1990 AND 1999">1990 - 1999    
    <?php

        //  else
            $sql = "SELECT * FROM Dvd_List";
            if (isset($_POST['year']))
            //  print("dsfsd");
            //  $sql = $sql." WHERE Year ".$_POST['year'].  " LIMIT $offset, $rec_limit";
                $_SESSION['year'] = $_POST['year'] ;
            
            else
                if(!isset($_GET['page']))
                
                    $_SESSION['year'] = "";
                
            //  print("none");
            //$sql = $sql." AND DVD_Title LIKE '%%'"
        //  $sql = "SELECT *" . "FROM Dvd_List " . "LIMIT $offset, $rec_limit";

        
            if(isset($_SESSION['year']))
            
                    $sql = $sql." WHERE Year ".$_POST['year'].  " LIMIT $offset, $rec_limit";
            

            else 
                $sql = "SELECT *" . "FROM Dvd_List " . "LIMIT $offset, $rec_limit";
            

            $retval = mysql_query($sql, $conn);
            if (!$retval) 
                die('Could not get data: ' . mysql_error());
            
 ?>
<input type="submit" name="submit" value="GO!">
</form>
 <form action='handlebasket.php' method='post' accept-charset='UTF-8'><?php
print "<table border='1'>
<th></th>
<th>Title</th>
<th>Price</th>
<th>Year</th>
";

    while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))//what u need is  create couple of these for each decade etc then sorround in if state
    
        print "<tr><td><input type=\"checkbox\" name=\"" . $row['ID'] .   "\"/></td><td>$row['DVD_Title']</td>
            <td>$row['Price']</td><td>$row['Year']</td></tr><br>";
    

    if ($page > 0) 
        $last = $page - 2;
        echo "<a href=\"$_PHP_SELF?page=$last\">Last 10 Records</a> |";
        echo "<a href=\"$_PHP_SELF?page=$page\">Next 10 Records</a>";
     else if ($page == 0) 
        echo "<a href=\"$_PHP_SELF?page=$page\">Next 10 Records</a>";
     else if ($left_rec < $rec_limit) 
        $last = $page - 2;
        echo "<a href=\"$_PHP_SELF?page=$last\">Last 10 Records</a>";
    
        mysql_close($conn);
?>
<input type="submit" name="submit" value="Add to cart">
</form>
</body>
</html>

【问题讨论】:

【参考方案1】:

将复选框值发送到 handlebasket.php

&lt;input type="checkbox" class='form' value="1" name="checkbox[]" /&gt;

&lt;input type="checkbox" class='form' value="2" name="checkbox[]" /&gt;

在handlebasket.php 中,编写下面的代码来获取这些值并显示

foreach($_POST['checkbox'] as $checkbox)

回显 $checkbox 。 '';

【讨论】:

喜欢这个打印 " $row['DVD_Title'] 这样改:while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))//what u need is create couple of these for each decade etc then sorround in if state ?&gt;&lt;tr&gt;&lt;td&gt;&lt;input type=\"checkbox\" name=&lt;?=$row['ID']?&gt; /&gt;&lt;/td&gt;&lt;td&gt;&lt;?=$row['DVD_Title']?&gt;&lt;/td&gt; &lt;td&gt;&lt;?=$row['Price']?&gt;&lt;/td&gt;&lt;td&gt;&lt;?=$row['Year']?&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br/&gt; &lt;?

以上是关于购物车,加入购物车按钮功能的主要内容,如果未能解决你的问题,请参考以下文章

zara小程序怎么加入购物车

jquery 实现加入购物车功能

购物车升级训练

淘宝怎么添加购物车

【CSS动画】饿了么加入购物车抛物线动画实现

综合应用 -- 购物车