ajaxを使用してデータベースを更新する際に問題があります。基本的に私はjQueryナンバースピナーを持っています。そのユーザーがその製品の番号をクリックすると、データベースにアップロードされます。代わりに、すべての製品がその番号に更新されるか、最後のものが更新されます。ユーザーが変更している製品だけをデータベースで変更できるようにするにはどうすればよいですか?ajax/jQueryを使用したデータベースへの更新
それは、私はjQueryの/ AJAXでの総noobのだが、私は学ぶことをしようとしていることは注目に値します。
小枝/ HTML:
{% extends '::base.html.twig' %}
{% block body %}
<h1 class="text-center"><u><i>Your Cart</i></u></h1>
<div class="container">
<div class="who_is_logged_in">
{% if user is null %}
<a href="{{ path ('fos_user_security_login') }}">Login</a>
{% else %}
<u>Hello<strong> {{ user }}</strong></u>
{% endif %}
</div>
<table class="table table-striped">
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Price Per Unit</th>
<th>Remove Product</th>
</tr>
</thead>
<tbody>
{% for key, product in quantity %}
<tr>
<td>{{ product.product }}</td> <!--Product-->
<td>
<input class="spinner" value="{{ product.quantity }}" style="width:30px">
</td> <!--Quantity-->
<td>${{ product.product.price|default('') }}</td> <!--Price-->
<td>
<a href="{{ path('product_remove', {'id': product.product.id }) }}">
<button name="REMOVE" type="button" class="btn btn-danger" id="removeButton">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</button>
</a>
</td><!--Remove-->
</tr>
{% endfor %}
</tbody>
</table> <!--top table-->
<div class="money-container">
<p class="text-right">Total Cost: ${{ totalCostOfAllProducts }}</p>
</div> <!--moneyContainer-->
{% for flash_message in app.session.flashbag.get('notice') %}
<div class="flash-notice">
<strong>{{ flash_message }}</strong>
</div> <!--flashNotice-->
{% endfor %}
</div> <!--container-->
<ul>
<li>
<a href="{{ path('product') }}">
Add More Products
</a>
</li>
<li>
<a href="{{ path('product_bought') }}">
Buy These Products
</a>
</li>
</ul>
<script type="text/javascript">
$(".spinner").spinner();
$('input.spinner').on('spinstop', function(){
min: 0
console.log('spinner changed');
var $this = $(this);
var value = $('.spinner').val();
var request = $.ajax({
url: "{{ path('product_quantityUpdate') }}",
method: "POST",
data : {
quantity: this.value,
}
}).done(function(result){
console.log('success', result);
}).error(function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(xhr.responseText);
console.log(thrownError);
});
});
</script>
{% endblock %}
のrelaventコントローラー機能:。
/**
* Updates quantity using ajax/jQuery request from showCart twig file
*
* @Route("/quantityUpdate", name="product_quantityUpdate")
* @Method("POST")
* @Template()
*/
public function quantityUpdateAction(Request $request) {
$em = $this->getDoctrine()->getManager();
$cart = $em->getRepository('ShopBundle:UserCart')->findOneBy(['user' => $this->getUser(), 'submitted' => false]);
$productsInUsersCart = $cart->getQuantities();
foreach ($productsInUsersCart as $key => $value) {
$productsInUsersCart = $value->getQuantity(); // Within the loop, gets the quantity of the product being changed
$value->getProduct()->getId(); //SHOULD be the product ID
$value->getUserCart()->getId(); //SHOULD be the user's cart ID
/* ----------------------------- Like Below & Will Update EVERYTHING in Users Cart ----------------------------- */
// dump($request->get('id_value'));
// dump($request->get('prodId')); die;
$value->setQuantity($_POST['quantity']);
$em->persist($value);
$em->flush();
}
/* ----------------------------- Like Below & Will Update the LAST product in Users Cart ----------------------------- */
// $value->setQuantity($_POST['quantity']);
// $em->persist($value);
// $em->flush();
return new Response ("This has been successful!");
}
実際のHTML出力がない場合は、アクションを現在の行に関連付ける必要がありますが、アクションを関連付ける方法はわかりませんが、 iquely各行を識別し、 –
は、私はこれを考え出したが、それは私が苦しんでんですよ。編集に自分のHTMLを追加します。行が私のアヤックスで取り出す方法を知らないidなので、私は苦労します。あなたの助けのための – Ale