2012-07-23 7 views
0

AJAX 전문가가 AJAX의 내 웹 사이트에서 업데이트 카트 기능을 구축하는 데 도움이되는 데 도움이 필요합니다.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 루프에 있지만 내가 여기, 그것은 문제가되지 않는다고 생각합니다.

$(".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)); 

} 

우리는, 그래서 어떤 업데이 트를 볼 수

는 다음 나는 Matei AJAX 기능을 설정했습니다. 누군가 내가 그 AJAX 기능을 설정하는 방법을 알아 내면 큰 도움이 될 것입니다.

+0

나는 당신이 여기에 너무 많은 질문을하고 있다고 생각합니다. 당신의 질문을 리팩터링 (좋은 수업처럼)하는 것은 하나의 중심적인 목적을 가지고 있습니다. – SomeKittens

+0

yeqh 네 말이 맞아, 그 주제를 약간 썼다 –

답변

1

jQuery 라이브러리 jQuery.post() method을 살펴 보시기 바랍니다. 코드

자바 스크립트 : 추가 정보를 위해

$("#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

고맙다. 그래서 내 첫 번째 함수를 사용하여 php 함수 (ok)를 호출 할 수 있지만 어떻게 Json을 사용합니까? 배열을 채우고 '},'json '이라고 써야합니다.);' 첫 번째 함수의 끝에서? –

+1

맞습니다. json은 $ .POST 메서드의 매개 변수이므로 PHP를 사용하면 올바른 형식의 데이터를 반환하기 위해 PHP 파일에서 json_encode() 함수를 사용해야합니다. –

관련 문제