2012-07-23 50 views
0

我真的需要一些帮助,从AJAX大师主导overthere,以帮助我在我的网站在AJAX建立我的更新购物车功能。如何使用Ajax更新我的购物车?

所以基本上,我想要做的是,当我修改我的产品之一,在一个input_dropdown什么,我的“update_cart”功能automaticaly打来电话,我的价格更新,以及我的输入

编辑:我重写我的问题,因为我取得了一些进展得益于马太

这是我的观点:

<?php 
      $options = array(
      '0' => '0', 
      '1' => '1', 
      '2' => '2', 
      '3' => '3', 
      '4' => '4', 
      '5' => '5', 
      '6' => '6', 
      '7' => '7', 
      ); 

     if($product['quantity']==0){ 
     $value[$product['title']] = set_value('quantity'.$product['title']); 
     }else{ 
     $value[$product['title']] = $product['quantity']; 
     } 
     $data0 = 'class="quantSelect" value="'.$value[$product['title']].'" id="quant'.$product['title'].'"'; 

     echo form_dropdown('quantity'.$product['title'], $options, $value[$product['title']],$data0); 
    ?&gt; 
    </td> 
    <td> 
    &lt;?php echo $product['price'] ?&gt; 
    </td> 
    <td id="&lt;?php echo 'price'.$product['title']?&gt;"> 
    $&lt;?php echo $total[$product['title']] ?&gt; 
    </td>[/code] 

嘛,一切都在一个foreach循环,但我想,在这里,也没关系。

然后我试图建立马太AJAX功能:

$(".quantSelect").click(function(){ 
    $.POST("&lt;?php echo base_url().'main/update_cart';?&gt;", 
      {product_id:$('&lt;?php echo $product['quantity']; ?&gt;').val(),quantity:$('&lt;?php echo 'quantity'.$product['title'] ?&gt;').val()}, 
      function(data){ 
       if(data.success) { 
        $("&lt;?php echo 'price'.$product['title']?&gt;").val(data.some_returned_value); // update value of an text input or textarea (view more info about jQuery selectors) 
        $("#totalPriceWithTaxes").html(data.some_other_returned_value); // update value of a paragraph      
       } 
      }, 'json'); 
}); 

,最后更新车功能:

function update_cart(){ 
$success = false; 
    if(!empty($_POST['product_id']) && !empty($_POST['quantity']) && is_numeric($_POST['quantity'])) { 

    // I get all the information i need here in order to calcul the final price 
    //We calcul the final price with taxes, shipping and everything. 
    $data['totalPriceWithTaxes'] = $data['tax'] + $data['totalPrice'] + $data['Shipping']->shipping; 
    $this->session->set_userdata('totalPriceWithTaxes', $data ['totalPriceWithTaxes']); 

    $success = true; 
    $some_returned_value = 69; 
    $some_other_returned_value = $data['totalPriceWithTaxes']; // the final price 
    } 
echo json_encode(array("success" => $success, 
     "some_returned_value" => $some_returned_value, 
     "some_other_returned_value" => $some_other_returned_value)); 

} 

我们在这里,所以我不能看到任何更新。如果有人能帮我弄清楚如何设置AJAX功能,我会深表感谢:)

+0

我想你在这里问的问题太多了。重构你的问题(像一个好的课堂)有一个主要目的。 – SomeKittens 2012-07-23 03:30:57

+0

yeqh你是对的,我重写了这个话题 – 2012-07-24 14:29:01

回答

1

我建议你看看jQuery库的jQuery.post() method

让我们来看看下面的例子:

Javascript代码:

$("#submit-button").click(function(){ 
    $.POST("/PATH_TO/update_cart.php", 
      {product_id:$('#product-id').val(),quantity:$('#quntity').val()}, 
      function(data){ 
       if(data.success) { 
        $("#form-field-id").val(data.some_returned_value); // update value of an text input or textarea (view more info about jQuery selectors) 
        $("p#form-p-id").html(data.some_other_returned_value); // update value of a paragraph      
       } 
      }, 'json'); 
}); 

欲了解更多信息有关jquery选择check this

PHP代码:

<?php 
    $success = false; 
    if(loged()) { // verify if the user is loged (if it's needed) 
     if(!empty($_POST['product_id']) && is_numeric($_POST['product_id']) && !empty($_POST['quantity']) && is_numeric($_POST['quantity'])) { 
      // use here your additional code 
      // update database 
      // if every condition is applied then confirm that the fields are updated 
      $success = true; 
      $some_returned_value = "data has been successfully updated"; 
      $some_other_returned_value = 45.3; // the final price 
     } 
    } 
    echo json_encode(array("success" => $success, 
          "some_returned_value" => $some_returned_value, 
          "some_other_returned_value" => $some_other_returned_value)); 
?> 

这是一个有关如何使用jQuery POST方法和PHP更新所需数据的简单示例。我没有使用任何代码,但您可以尝试像这样更新购物车。 jQuery是一个强大的库,所以我建议你看看它。

+0

谢谢,所以,我可以用你的第一个函数来调用我的price()php函数,好的,但是我怎么用Json呢?我必须简单地填充该数组,然后写下'},'json');'在第一个函数的结尾? – 2012-07-24 14:37:24

+1

你是对的,json是$ .POST方法的一个参数,所以如果你使用它,那么你必须在php文件中使用函数json_encode()来返回正确类型的数据 – 2012-07-25 06:58:39