0

我真的需要一些 AJAX Guru 大师的帮助,以帮助我在我的 AJAX 网站上构建我的更新购物车功能。

所以基本上,我想做的是,当我在一个 input_dropdown 中修改我的一个产品时,我的“update_cart”函数被自动调用,我的价格以及我的输入都会更新

编辑:我重写了我的问题,因为感谢 Matei 我取得了一些进展

这是我的观点:

<?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 循环中,但我认为在这里,没关系。

然后我尝试设置 Matei 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 功能,我将不胜感激:)

4

1 回答 1

1

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

让我们看看下面的例子:

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 选择器的更多信息,请查看这里

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 是一个强大的库,所以我建议你看看它。

于 2012-07-23T07:55:24.573 回答