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 _ );
    
    评论

报告相同问题?

悬赏问题

  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥20 和学习数据的传参方式,选择正确的传参方式有关
  • ¥15 这是网络安全里面的poem code
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路
  • ¥15 内网办公电脑进行向日葵