Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter + jQuery + Ajax + Dynamic Form Select (Drop-Down) Repopulation
#1

[eluser]Unknown[/eluser]
I have two forms, one for adding albums to a database and one for adding artists.

The albums form has a select (drop-down) menu to choose an artist (dynamically populated from the database) and a text input field for the album name.

The artists form has a text input field for the artist name.

I would like to give the user the ability to add an artist to the select menu on the albums form without leaving or refreshing the page (via ajax).
I have the php side of things dialed in, with validation, graceful degradation, etc.
I have setup functionality to pull in the artists form to the page using the jquery toggle method when the user requests. All is well.
However, I am having difficulty repopulating the artists select menu in the albums form when a new artist is added.

I would like to know if anyone out there has advice as to the available strategies to handle this ...

Currently I have tested two options:

The first using jquery to prepend a new option to the existing select menu (which is populated by when the page first loads).
This strategy is okay and gets the job done, but not as elegant because the options are not sorted in the desired order.
I realize that I can re-sort after the new option is added, but the order is complicated and this does not "feel" right to me.

The second strategy, involves calling a controller method to get all artists (after the new artist has been submitted), then putting the results to a view file and using ajax to integrate the view into the album form.
This method also "works" and it reuses the same method to populate the artists select as during the initial page load (this seems be right).
I can fork this method to return results by detecting if the method is called by an ajax request, then alter the results returned for ajax.
Here is where I struggle ...

I am using the 960 grid system and the html markup is already pretty heavy, so creating an additional file simply to handle a select menu does not "feel" like a concise approach.

I hope this is clear enough and I realize this is difficult without seeing any code, but I am more interested in the strategies available, more than the coding at this point.

Thanks for your time!




Theme © iAndrew 2016 - Forum software by © MyBB