0

AJAXのWebサイトで更新カート機能を構築するために、AJAXGuruマスターの助けが本当に必要です。

つまり、基本的に、1つのinput_dropdownで製品の1つを変更すると、「update_cart」関数が自動的に呼び出され、入力と同様に価格が更新されます。

編集:マテイのおかげで進歩したので質問を書き直します

これが私の見解です:

<?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ループにありますが、ここでは問題ではないと思います。

次に、MateiAJAX関数を設定しようとしました。

$(".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 Selectorsの詳細については、こちらを確認してください

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

これは、jQueryPOSTメソッドとPHPを使用して必要なデータを更新する方法についての簡単な例です。私はあなたのコードを使用しませんでしたが、あなたはこのようにあなたのカートを更新することを試みることができます。jQueryは強力なライブラリなので、ぜひご覧になることをお勧めします。

于 2012-07-23T07:55:24.573 に答える