0
나는 Silex를 사용하여 작성한 MVC와 매우 사용하기 쉬운 쇼핑 카드를 만들고 있습니다. JS/AJAX 문제가 있습니다. 바구니에 제품을 제출하려고합니다. 문제는 세부 사항을 제출하고 세션에서 설정하고 잘 작동한다는 것입니다. 두 번째 제품을 추가하면 배열에 추가하는 대신 제품을 대체합니다. 나는 여러 가지 일을 시도했지만 성공하지 못했습니다. (아래 코드).Silex : 세션 도우미에 새 값 추가
건배,
이완 아약스 값을 사용
라우터 :
$app->get('/add/to/cart/{id}/{name}/{price}', function($id, $name, $price) use ($app) {
$basket[] = array (
'id' => $id,
'name' => $name,
'price' => $price
);
$app['session']->set('basket', $basket);
return new Response("Added to basket.");
});
여기에 자바 스크립트
$('.add-to-cart').on("click", function() {
$productId = $(this).attr('product-id');
$productName = $(this).attr('product-name');
$productCost = $(this).attr('product-cost');
$.ajax({
type: "GET",
url: "http://localhost/php/Test/web/index.php/add/to/cart/"+$productId+"/"+$productName+"/"+$productCost,
data: {
// Doesn't need the data, Silex takes it from the url
},
success: function() {
// Just to check it worked
console.log("add/to/cart/"+$productId+"/"+$productName+"/"+$productCost);
},
error: function() {
}
});
});
array_merge도 사용해야했지만 spot-on! 고마워, 지금은 분명해 보인다. 너는 나에게 많은 시간을 절약 해 줬어! –