需要调整我的代码


need to ajaxify my code

我需要调整此代码并删除php中的头位置选项。我希望用户在点击添加到购物车按钮后保持在同一页面上。。。。。非常感谢您的帮助我的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();
}
?>

显示产品

<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']?>)" />

我购物车的一部分

<?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';
        }
    }
 }
?>

下面是我的总结。您需要对ajax部分中的URL以及如何处理返回数据进行一些更改。JS Fiddle:http://jsfiddle.net/fzzcdsa7/

代码:

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