0

I hope this hasn't been answered somewhere :)

I am using the follwing form input field to allow customers to update the quantity of items in their basket:

<input type="text" name="item_qty[<?php echo $product['product_id']; ?>]" value="<?php echo $product['product_qty']; ?>">

So each item in the basket has an input field as above and there is one submit button to update all product quantities in one go.

My problem is that I cannot work out how to create the required array in jquery to send through ajax using the array in the name element (I want to keep it like this if possible as I want to ensure that the form will work as a normal submit to the PHP file if there is any reason why the jquery can't be initialised).

I have thought that maybe the name should be the key from the basket array but not sure. Can anyone advise of the best way to do this and if it is to keep the input field as it is now, how I get the required data to create an array in jquery please.

3
  • 2
    Try $('form').serialize(); Commented Jun 20, 2013 at 7:57
  • 1
    Google is your friend: tutorial here Commented Jun 20, 2013 at 8:02
  • I have Googled - many times using different terms and I have just looked at that tutorial and it doesn't help with my question. I already use jquery / ajax successfully on normal form submissions and know how to create the array to send through ajax. My problem is creating the array from the array in the name element of the input field (and serialize doesn't work) Commented Jun 20, 2013 at 8:13

3 Answers 3

0

So, if I understand you correctly you have some output similar to this?

<input type="text" name="item_qty[0]" value="4">
<input type="text" name="item_qty[1]" value="1">
<input type="text" name="item_qty[2]" value="3">
<input type="text" name="item_qty[3]" value="7">

and you want a result with something like:

[{item_qty[0], 4}, {item_qty[1], 1}, ...]

If this is correct, I've thrown something together in this fiddle: http://jsfiddle.net/N6eBZ/

EDIT: For the associative array, you need to create a JS Object, see this fiddle: http://jsfiddle.net/KyleMuir/N6eBZ/1/

Hope this helps

3
  • Thank you Kyle, that is virtually what I am looking for but I am now stuck as how to get the values into an associative array like {item_qty[0] : 4} to send through ajax. What I get when I output to console (without the toString) is ["item_qty[4]", "3", "item_qty[1]", "2"] Commented Jun 20, 2013 at 9:00
  • Kyle, thank you - I got it sorted in the end using your script, just had to faff for ages getting the PHP to work but all sorted now. Commented Jun 20, 2013 at 15:40
  • Cool, glad to hear it helped. Commented Jun 20, 2013 at 19:22
0

var obj = {};

`$('input').each(function(index, value){ obj[this.name] = this.value;

});`

0

I've forked the accepted answer and rewrote it to use $.map(), which may be more appropriate than $.each(). $.map() is intended for this sort of application: building an array from the results of a DOM query:

var results = $('[name^=item]').map(function() {
    var $this = $(this),
        jsObject = {};

    jsObject[$this.attr('name')] = $this.val();
    return jsObject;
}).get();

Here's a Fiddle.

Here's the documentation for $.map

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.