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 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载