Updating multiple select boxes in Symfony using AJAX

For a client, I had to present a bunch of select boxes where users could select a genre, then a sub-genre based on your genre of choice, then a second level of sub-genres based on the selected sub-genre.

For a client, I had to present a bunch of select boxes where users could select a genre, then a sub-genre based on your genre of choice, then a second level of sub-genres based on the selected sub-genre. My first choice was to use the symfony AJAX options, and updating the sub-genre select box with the observe_field function. This worked, the sub-genre got updated as I selected the main genre. However, for some reason, when selecting a sub-genre, the list with the second level of sub-genres was not updated. Weirdness! And then, in some bright moment, I decided to try and only update the options inside the sub-genre selectbox. And behold, it now worked. So, I am using observe_field to observe the genre selectbox, and another observe_field to observe the sub-genre selectbox. As a genre is selected, the options of the sub-genre are being reloaded. As a sub-genre is selected, the 2nd level sub-genres are being reloaded. It's a little trick, and I'm still not sure why my initial approach fails (as I took care that the replacing selectbox had the same name/id as the original one with no options). However, it works and it's a clean solution I guess.
Add comment

Comments

gravatar Bert-Jan: I had a strange problem a few months back. In a div I had two droppable area’s as part of a form. When the form was submitted with ajax and the div was refreshed, the droppables needed to be declared again. No problem, just stuff the droppable_element call inside the ajax response and set script=true and all went well.. except in IE6/7. It seems when you redeclare a droppable prototype creates a javascript problem and you can’t drag things anymore as it will give a script error immediately.
After a few hours playing with it I found the solution is to remove the droppable (Droppable.remove(‘div’ ;) IIRC) before renewing the element. Suddenly, everything worked perfectly. The same does not apply to draggables, but I guess it’s good practise to keep the scripting model as clean as possible.


August 14, 2007
gravatar stefan: Well, the (IMHO) great feature of symfony is that you don’t need to code a single letter of Javascript anymore to create wonderful rich GUI’s. However, still you’ll have trouble like above I’m afraid, so you still need to be able to think “out of the box” I guess.


August 14, 2007
gravatar Cucumber Jones: Stefan, can you please drop me an email regarding Metal Machine Music Live/ Thanks.


August 17, 2007
gravatar excessivedemon: I was able to achieve this effect using another method.. I’m not sure how I to call it.. didn’t use any observe_fields or such.. just a plain basic recursive call, which is really really very efficient.. you can check it at http://www.opets.com – create an account with any username and password.. then create a pet.. you’d see a bunch of categories and sub categories and sub sub categories to the nth level.. and they get updated via AJAX whenever 1 of the select boxes change values.. o.. and here’s another one : http://euroxtrade.baguiocommunity.com/web/auto/create

I can post the codes if anyone wants to have a look at it..


August 21, 2007
gravatar Isi: excessivedemon,

I wouldn’t mind seeing this code. This is something many of us have come across here and there and would be nice to see how you approach it vs my approach vs Bert-Jan above.

Thanks


August 24, 2007
gravatar : :evil: 8)
March 29, 2008
gravatar :
September 4, 2009
gravatar Bart: Hello! I have a problem with this. Can you give me the solution?
September 17, 2009
gravatar orlando personal injury attorneys: I love using these options. There is so much that you can get from this. The options are endless.
November 3, 2011
gravatar Backlink Indexing Software: "For a client, I had to present a bunch of select boxes where users could select a genre, then a sub-genre based on your genre of choice, then a second level of sub-genres based on the selected sub-genre. My first choice was to use the symfony AJAX options, and updating the sub-genre select box with the observe_field function. This worked, the sub-genre got updated as I selected the main genre. However, for some reason, when selecting a sub-genre, the list with the second level of sub-genres was not updated. Weirdness!"

Strange indeed! I thought about this and realized it could be a result of a seg fault if you're coding AJAZ by hand...?


January 26, 2012
gravatar What Is Walking Pneumonia: I wouldn’t mind seeing this code. This is something many of us have come across here and there and would be nice to see how you approach it vs my approach vs Bert-Jan above.

February 9, 2012

Php5_zce_logo

Upcoming events

I will be speaking 17-02-2012: Techademy Trainingday February
I will be speaking 23-02-2012: Zend Webinar: Git for Subversion Users

Tags

1337 2008 2010 2011 4developers access modifiers accessibility AdaLovelaceDay09 advent agavi agile alfred amsterdam apache api apple article articles atk atkMetaNode audioscrobbler automation azure backwards compatibility barcelona barcodes bash bbc bbq beatstad belgium best practices bittorrent blogging blogs boards of canada book books bughuntday bundle caching cake cal evans calendar career cat cerf certificate cfp clear cms cologne common sense communities community components conference conferences contest continuous integration contribute contribution crisis css custom d-day datetime DbFinderPlugin decorator decorators deployment devdays development directoryindex docblox doctrine documentation download dpc dpc09 dpc10 dpc11 DPC2008 dreamhost drupal dv7 eclipse ed editors efficiency enterprise errors event events expertise ezcomponents facebook finland flickr fork framework frameworks freelance freeze frontend fun game games geoip germany getting real git github gnome-do google google calendar googletalk graceful degradation hack hackers hidden gem hiphop howto hp HR html http i386 ibuildings icann ide ideasofmarch idm imovie indy ingewikkeld integration international php conference internet interview ipad IPC ipc ipc08 ipc10 ipc11se iterm2 javascript jenkins jenkins-php job job openings jobeet john peel joomla joomladays kiva kubuntu launcher launchy left on the web libraries library lighttpd lime linktuesday linux live london loudblog m2ts mac magazines malware mambo marjolein mediterra meeting meme meta methodology micro-financing microframework microsoft migration movie music mysql namespace namespaces netbeans netherlands newsfire nllgg nos odmarco open source opinion ORM osx paradiso paris partnership pavilion pear pecl performance personal pfc10 pfc11 pfcongres pfcongrez pfz photo php php5.3 phpabstract phpazure phpBB phpbb phpbelgium phpbenelux phpbnl10 phpday phpdoc phpdocumentor phpgg phpitalia phpnw phpnw08 phpnw11 phpstorm phptek phptek09 phpuk2009 phpUnderControl phpunit php|architect php|tek podcast politics portability postcrossing presentation presentations private projects protected prototype PSR-0 public python qa qr codes re2c recruiting refactoring review rewrite ruby on rails san francisco schedule scifi script security sensio seven things sfdaycgn sflive2011 shell scripting silex simplexml slides smfony software sogeti solar sound speakers spl ssh standard standards star trek static steer strings stylesheets subversion symfony symfony live Symfony2 symfonycamp symfonyday symfonylive symfonyUnderControlPlugin talk talks techademy technology techportal tek09 telecommuting terratec terrorism testfest testing textmate textpattern the right tool timeout tips tld todo tomas tools training twig uncon unet usability usergroup validation vhost video vim vinyl virus warp webinar weblogging webservices wiki windows winphp women wordpress work workshop world world of warcraft wpi writing wunderlist xml xpath xsd yara year youtube zc11 ZCE zemanta zend zend framework zend server zend studio zendcon Zend_Form zite
© 2004 - 2012 Stefan Koopmanschap + Powered by Symfony, photos powered by Flickr, links powered by Delicious, Shanghai smilies by Iconbuffet. Feeds: rss / atom. Left on the Web v4.4.0.1