0

I have a webpage with the following input:

<input type='text' name='packagedims[]' value='1' id='packagedims' title='Package1' />

I'm using it in jQuery as so:

var packagedims = $('input#packagedims').serialize();

and then I'm using jQuery.ajax to post it to my php file:

'&packagedims=' + packagedims

(I know I could use Serialize() on the whole form but I'm modifying existing code so haven't got round to doing that yet. In the above line I'm sending individual variables to php.)

When I test the output using alert() I get the following:

&packagedims=packagedims%5B%5D=1&packagedims%5B%5D=2

I just wanted to check that this sort of output is correct and that using unSerialize() in my php file will be able to split this into a proper array.

Thank you,

2
  • Shouldn't it be like packagedims%5B0%5D=1&packagedims%5B1%5D=2? (index included) Commented Jul 28, 2011 at 7:20
  • that's what I was thinking actually... Commented Jul 28, 2011 at 17:15

1 Answer 1

4

.serialize() [docs] already returns a string in the form of

key=value&key=value&...

Don't append to something else, otherwise it becomes

key=key=value&...

which is incorrect. So do not concatenate it with '&packagedims='.

There is no need to use any unserialize function in PHP. The data will be sent as GET or POST data and be available in PHP through $_GET or $_POST. E.g. if you make a POST request, $_POST['packagedims'] will be an array containing the values.

Variables From External Sources might be worth a read.

Sign up to request clarification or add additional context in comments.

3 Comments

Excellent, thanks for that I have amended my code and will read up on what you have advised!
Should there be an index value between the %5D%5B values?
@rav: No, the names of the fields are name='packagedims[]'. jQuery will not not add anything to the name. And in PHP, if you want to add something to an array, you just write array[] = something so that is totally fine.

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.