Handling PHP Form Arrays with jQuery
31 Mar
Posted by: trent in: Impromptu, Javascript, PHP, Programming
I’ve ventured into new waters lately with CakePHP, which may I add is a splendid framework. One thing I quickly ran across is how Cake may handle forms with it’s form helpers. Using the helpers it will write your html for you, but doing so it uses arrays in its form fields. This is a first for me dealing with jQuery and these arrays. If you’re confused about what I’m talking about take a look:
<input type="text" name="data[modelName][fieldName]" id="modelField" value="" />
By generating the form fields this way Cake can instantly figure out what field this is and where it goes. The problem comes in when you need to do a few fancy ajax additions and pass this puppy into $.post(). With this function jQuery is expecting a one dimensional object, there is no need to layer your object to match this array. So that being said I would create an object like the following and pass it in:
//build your object using strings as the key names
var obj = {
'data[modelName][fieldName1]' : 'asfasd',
'data[modelName][fieldName2]' : 123,
'data[modelName][fieldName3]' : 'bla bla bla'
};
//make the ajax post
$.post('myurl/whatever/', obj, function(data,textStatus){
$.prompt('item sent!');
});
As you can see you treat the entire html name array as a string within your javascript object. On the retrieving end php will then know exactly what to do with it. Not going to lie this stumped me at first, but after a few trial and error attempts it proved to be fairly simple.
And in case you’re wondering I was using this with Impromptu and it didn’t like the field names when gathering them like this(really the jQuery selectors I believe, but I will need to do more digging). Hope this helps someone as much as it did me!
Related posts:


5 Responses
Kfir Gollan
31|Mar|2009 1If you are looking for a new framework I highly recommend symfony
http://www.symfony-project.org/
trent
31|Mar|2009 2I’ve heard a lot of good things about symfony, but have yet to play with it. I’ve used CodeIgnitor and Django in the past and those two are not bad either. I am very impressed with Django, its only downfall is that it needs to be installed to use it compared to php frameworks.. I’ve been helping on a project which was developed with CakePHP and I’ve been pretty pleased with it so far. My next framework to check out may just be Symfony!
Николай
10|May|2009 3Очень интересная статья! ;)
AJ de Waal
26|Aug|2009 4Hi Trent,
I was looking at your posts for Cakephp and impromtu but cannot figure out how exactly everything fits together.
I would like to click on a link/button to bring up the impromptu with a form which should be submitted via ajax if thats possible.
If you have a spare moment would you mind sending me an example.
I have previously tried to implement impromtu for the messageFlash as well but could not get that to work either :(
any help would be great.
thanks for your time,
AJ
trent
26|Aug|2009 5I have a couple examples of forms on the docs page. Check these two out:
http://trentrichardson.com/Impromptu/demos/demo1.php
http://trentrichardson.com/Impromptu/demos/demo2.php
Leave a reply