Welcome Guest, Not a member yet? Register   Sign In
[solved] Ajax cart updating
#1

[eluser]umbungo[/eluser]
I have a (working) form to update my cart as;
Code:
//form open, id=cart_update

foreach($this->cart->contents() as $items){
    $id = $items['id']; $rowID = $items['rowid']; $qty = $items['qty']; $name = $items['name'];
    
    echo form_hidden('basket['.$id.'][rowid]', $rowid);
    echo '&lt;input type="text" name="basket['.$id.'][qty]" value="'.$qty.'"&gt;'.$name.'<br/>';
}

//submit button
This passes an array which i can put into a foreach loop to get values for multiple products and allows updating multiple items at once with one button.

I am trying to add ajax functionality. When adding items I had an add button and form for each item, and the following worked great (Jquery);
Code:
$("form.add").submit(function() {

    var id = $(this).find('input[name=product_id]').val();
    var qty = $(this).find('input[name=quantity]').val();

    // POST, GET, HTML functions to update page.
    return false;
});

But i'm not sure how to work with multiple items here.. I can't do something like add a hidden form (name='id', id=$id) because it would just be overwritten and always point to the last item. ??

I'm also not sure how to get a value of an item which is part of an array..
I attempted to retreive the values from the forms (as top) by setting a test value for 'id' in the JS, but the following still didn't work;
Code:
var id = '1234';

            var id = $(this).find('input[name=basket\[' + id + '\]\[rowid\]]').val();
            var qty = $(this).find('input[name=basket\[' + id + '\]\[qty\]]').val();
#2

[eluser]Eric Barnes[/eluser]
Why not just pass all the form values?
Code:
var data = $("form.add").serialize();
#3

[eluser]umbungo[/eluser]
I think i have found the 'real' problem.. it doesn't work because part of the form is generated by ajax.

I have tried

Code:
$("form#cart_update").submit(function() {
            alert($(this).serialize())
and also
Code:
$("form#cart_update").live('submit',function(e){
            e.preventDefault();
            alert($(this).serialize());
and both return as empty (unless i ensure there is no ajax used in creating the parts of the form, in which case they work).
#4

[eluser]umbungo[/eluser]
Solved it.. the problem was that the &lt;form&gt; and &lt;/form&gt; elements were not on the same level of the DOM (along with the use of live). This didn't cause problems with a normal load.. but with an ajax load it did.




Theme © iAndrew 2016 - Forum software by © MyBB