weixin_33692284 2015-02-04 14:21 采纳率: 0%
浏览 1

需要整理我的代码

i need to ajaxify this code and remove header location option in php. i want the user remain on same page after clicking add to cart button .....any help is highly appreciated My javascript

<script language="javascript">
function addtocart(pid){
    document.form1.productid.value=pid;
    document.form1.command.value='add';
    document.form1.submit();
}
</script>


<?php 
if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){
    $pid=$_REQUEST['productid'];
    addtocart($pid,1);
    header("location:shoppingcart.php");
    exit();
}

?>

displaying products

<form name="form1">
<input type="hidden" name="productid" />
<input type="hidden" name="command" />
</form>

<?php echo $row['picture']?>" />
<b><?php echo $row['name']?></b><br />
<?php echo $row['description']?><br />
Price:<big style="color:green">
$<?php echo $row['price']?></big><br /><br />
<input type="button" 
value="Add to Cart" onclick="addtocart(<?php echo $row['serial']?>)" />

part of my shopping cart

<?php 

if($_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
    remove_product($_REQUEST['pid']);
}
else if($_REQUEST['command']=='clear'){
    unset($_SESSION['cart']);
}
else if($_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';
        }
    }
 }

?>
  • 写回答

1条回答 默认 最新

  • weixin_33720078 2015-02-04 14:32
    关注

    So, here's what I've put together. You'll need to make a few changes as to the URL in the ajax part, how the return data is handled. JS Fiddle: http://jsfiddle.net/fzzcdsa7/

    Code:

    <form name="form1" id="form1">
    <input type="hidden" id="productid" name="productid" />
    <input type="hidden" id="command" name="command" />
    </form>
    
    function addtocart(pid){
        $("#productid").val(pid);
        $("#command").val('add');
        ajaxSubmit();
    }
    function ajaxSubmit() {
        $.ajax({
          type: "POST",
          url: "mypage.php",
            data: {"productid": $("#productid").val(), "command": $("#command").val()},
            success: function(returnedData) {
                 alert(returnedData);   
            }
        });   
    }
    addtocart(12); // addtocart( _ ID _ );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64