Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
2  
Try $('form').serialize(); –  u_mulder Jun 20 at 7:57
1  
Google is your friend: tutorial here –  Mike W Jun 20 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) –  The Baker Jun 20 at 8:13
add comment

2 Answers

up vote 0 down vote accepted

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

share|improve this answer
 
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"] –  The Baker Jun 20 at 9:00
 
@user1260846 See edit. –  Kyle Muir Jun 20 at 9:15
 
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. –  The Baker Jun 20 at 15:40
 
Cool, glad to hear it helped. –  Kyle Muir Jun 20 at 19:22
add comment

var obj = {};

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

});`

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.