Title: Use AJAX to update records in database
Tags: php;ajax
Question: I have a shopping cart website where the user can add items to his cart. For each item, I have a drop-down list where the user can update the number of items. This is the loop where I show the drop-down list, and marks selected the quantity the user has added to his cart. (quantity available is for the amount of products, while quantity is for the user amount of that product).
cart.php
echo '<select name="product' . $prod_row["product_id"] . '"&gt;';
for ($i = 1; $i <= $prod_row["quantity_available"]; $i++) {
echo '<option value="' . $i . '"';
if ($i == $cart_row["quantity"]) {
echo ' selected="selected"';
}
echo '&gt;' . $i . '</option&gt;';
echo '</select&gt;';
On the drop-down list onchange I wish to make an AJAX call to update the quantity. I have this Javascript method which I took from W3Schools.
function updateCartQuantity(qty, cartid)
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200)
{
}
}
xmlhttp.open("GET","updatecart.php?qty="+qty+"&amp;cartid="+cartid,true);
xmlhttp.send();
</script&gt;
How can I modify this Javascript method to cater for the update of a record?
Comment: All the update occurs in the `updatecart.php`. Or it isn't the right logic?

Comment: It is not carrying out the update. This is the content of my `updatecart.php`

Comment: update_ShoppingCartQuantity($_GET['cartid'], $_GET['qty']);

Comment: I have used the `onchange` event where I call the updateCartQuantity() function.

Comment: It is working now. I made a silly mistake. In the `updatecart.php` I was calling a class which I hadn't referenced to. Thanks for your help and utmost availability :)

Comment: how are you updating the cart in your website? do u have any cart.php?

Comment: it is perfectly fine, i was just curious if you had a separate updatecart.php or not? your code seems to be perfect, what problem are you facing?

Comment: when you select any item from the list, are you calling OnChange() event in javascript? when OnChange() event is fired then call updateCartQuantity by passing the qty and cartid.

Comment: is your onChange event getting fired, place an alert() box there and debug each step, your code is both perfect in javascript side and php side, some minor problem you need to debug it.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 9, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:01/09/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><ajax>', 'Title': 'Use AJAX to update records in database', 'ViewCount': 526, 'id': 8789550, 'user_AboutMe': '', 'user_AccountId': 1155252.0, 'user_DisplayName': 'chris05', 'user_Reputation': 703.0, 'user_Views': 42.0, 'user_id': 1136044.0}}