php购物车在服务器上运行不正常

时间:2014-11-07 10:38:50

标签: php

我已经在php中创建了一个实现会话的购物车,但是当用户点击服务器上详细信息页面上的“添加到购物车”按钮时,它不会重定向到cart-page.php并且在localhost.i上工作正常认为标题功能不正常请帮我找出问题。

这是我的代码。

enter code here

    <div id="central_content"> 
   <?php

    include("include/db.php");
    include("include/functions.php");
    error_reporting(E_ALL);
ini_set("display_errors", 1);
    if(isset($_REQUEST['command']) && $_REQUEST['command']=='add' && $_REQUEST['productid']>0){
        $pid=$_REQUEST['productid'];
        addtocart($pid,1);
        session_start();
        if($_SESSION['login']=="loggedin")
        {
        header('location:cart-page.php');
        }
        else
        {

            ?>
            <script>
        document.getElementById("logindiv").innerHTML="welcome";
            </script>

        <?php 
        header('location:login-page.php');
                }
        exit();}
?>
<script language="javascript">
    function addtocart(pid){
        document.form1.productid.value=pid;
        document.form1.command.value='add';
        document.form1.submit();
    }
</script>


<body>
<form name="form1">
    <input type="hidden" name="productid" />
    <input type="hidden" name="command" />
</form>
<div align="center">
    <h1 align="center">Henger2</h1>
    <table border="0" cellpadding="2px" width="600px">
        <?php

            $result=mysql_query("SELECT * FROM `products` WHERE serial=1");
            while($row=mysql_fetch_array($result)){
        ?>
        <tr>
            <td></td>


            <td> <div class="product_item"> <div class="product_left"> <a href="<?=$row['picture']?>" class="simple_image" title=""><span class="product_zoom" style="display: none;"><img src="styles/Images/ico-zoom.png" alt="Zoom Picture" title="Zoom Picture" style="border:none;"/></span><img src="<?=$row['picture']?>" alt="Image 1" title=""/></a>
            <div class="product_thumb_container">  
            </div>
          </div>
           </div>
          </td>

              <td> 

              <div class="product_right">

          <div class="product_data">Product:</div>
          <div class="product_data grey"><?=$row['name']?></div>
          <div class="div_br"></div>
          <div class="product_data">Product-Softwares:</div>
          <div class="product_data grey"><?=$row['description']?></div>
          <div class="div_br"></div>
          <div class="product_data">Software-Format:</div>
          <div class="product_data grey"><?=$row['desc']?></div>
          <div class="div_br"></div>
          <div class="product_data">Price:</div>
          <div class="product_data grey">$<?=$row['price']?></div>
          <div class="div_br"></div>    


           <input type="submit" value="Add to Cart" onclick="addtocart(<?=$row['serial']?>)" />

            </td>
        </tr>
        <tr><td colspan="2"></td>
        <?php } ?>
    </table>
</div>

             cart-page.php




     <script language="javascript">
    function del(pid){
        if(confirm('Do you really mean to delete this item')){
            document.form1.pid.value=pid;
            document.form1.command.value='delete';
            document.form1.submit();
        }
    }
    function clear_cart(){
        if(confirm('This will empty your shopping cart, continue?')){
            document.form1.command.value='clear';
            document.form1.submit();
        }
    }
    function update_cart(){
        document.form1.command.value='update';
        document.form1.submit();
    }


</script>
 <?php

    include("include/db.php");
    include("include/functions.php");

    if(isset($_REQUEST['command']) && $_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
        remove_product($_REQUEST['pid']);
    }
    else if(isset($_REQUEST['command']) && $_REQUEST['command']=='clear'){
        unset($_SESSION['cart']);
    }
    else if(isset($_REQUEST['command']) && $_REQUEST['command']=='update'){
        $max=count($_SESSION['cart']);
        for($i=0;$i<$max;$i++){
            $pid=$_SESSION['cart'][$i]['productid'];
            $q=intval($_REQUEST['product'.$pid]);
            if($q>0 && $q<=999){
                $_SESSION['cart'][$i]['qty']=$q;
            }
            else{
                $msg='Some proudcts not updated!, quantity must be a number between 1 and 999';
            }
        }
    }


?>
<script language="javascript">
    function del(pid){
        if(confirm('Do you really mean to delete this item')){
            document.form1.pid.value=pid;
            document.form1.command.value='delete';
            document.form1.submit();
        }
    }
    function clear_cart(){
        if(confirm('This will empty your shopping cart, continue?')){
            document.form1.command.value='clear';
            document.form1.submit();
        }
    }
    function update_cart(){
        document.form1.command.value='update';
        document.form1.submit();
    }


</script>

<form name="form1" method="post">
<input type="hidden" name="pid" />
<input type="hidden" name="command" />
    <div style="margin:0px auto; width:600px;" >
    <div style="padding-bottom:10px">
        <h1 align="center">Your Shopping Cart</h1>
    <input type="button" value="Continue Shopping" onclick="window.location='projects.php'" />
    </div>
        <div style="color:#F00"></div>
        <table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; background-color:#E1E1E1" width="100%">
        <?php

            if(is_array($_SESSION['cart'])){
                echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td>Serial</td><td>Name</td><td>Price</td><td>Qty</td><td>Amount</td><td>Options</td></tr>';
                $max=count($_SESSION['cart']);
                for($i=0;$i<$max;$i++){
                    $pid=$_SESSION['cart'][$i]['productid'];
                    $q=$_SESSION['cart'][$i]['qty'];
                    $pname=get_product_name($pid);
                    if($q==0) continue;
            ?>
                    <tr bgcolor="#FFFFFF"><td><?=$i+1?></td><td><?=$pname?></td>
                    <td>$ <?=get_price($pid)?></td>
                    <td><input type="text" name="product<?=$pid?>" value="<?=$q?>" maxlength="3" size="2" /></td>                    
                    <td>$ <?=get_price($pid)*$q?></td>
                    <td><a href="javascript:del(<?=$pid?>)">Remove</a></td></tr>
            <?php                   
                }
            ?>
                <tr><td><b>Order Total: $<?=get_order_total()?></b></td><td colspan="5" align="right"><input type="button" value="Clear Cart" onclick="clear_cart()"><input type="button" value="Update Cart" onclick="update_cart()">



                </td></tr>
            <?php
            }
            else{
                echo "<tr bgColor='#FFFFFF'><td>There are no items in your shopping cart!</td>";
            }
        ?>
        </table>
    </div>
</form>

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
 <input type="image" src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" style="margin-left:500px; margin-top:20px;">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="pankaj.gargas@gmail.com">
<?php
            if(is_array($_SESSION['cart'])){
                $max=count($_SESSION['cart']);
                $a=1;

                foreach($_SESSION['cart'] as $item){
                $name=get_product_name($a); 


            ?>
<input type="hidden" name="item_name_<?php echo $a; ?>" value="<?=$name; ?>">
<input type="hidden" name="quantity_<?php echo $a; ?>" value="<?= $item['qty']; ?>">
<input type="hidden" name="amount_<?php echo $a; ?>" value="$ <?=get_price($a)?>">
<?php 
$a++;
}
            }?>


</form>

1 个答案:

答案 0 :(得分:0)

此代码无效。有session_start()和重定向,但之前有输出:<div id="central_content">

我假设之前有<html>和其他HTML标记。

移动你的这些代码,因为你做了任何输出。

header('location ...');之后添加die();