Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter/JQuery/Ajax: Set dropdown based on previously selected value
#11

[eluser]theprodigy[/eluser]
you will want to put it in the document.ready function. Otherwise it will process right away, and that element may not exist (or fully exist) quite yet.
#12

[eluser]Aken[/eluser]
You want to set the value PRIOR to rendering all of the options within it? How can you be doing an Ajax call when your HTML isn't even populated yet? I think you're explaining things differently than you intended. Maybe you can let us know how your loading and actions are structured. For example:

1) Page loads. Drop down contains all possible values.
2) Ajax code is run when a user does so-and-so. A response is given with the value.
3) Drop down is updated with the returned value selected.

Also, definitely put your jQuery code within the document ready as theprodigy suggested.
#13

[eluser]ShawnMA[/eluser]
My apologies for any confusion. Here is (hopefully) a clarified explanation of how my process is working. Also, if it helps, I've built my app thus far leveraging this tutorial: http://www.ifadey.com/2010/07/crud-using...niter-iii/

1) Users clicks on "update" link, which has corresponding record ID in url.
2) This calls to my .js page, which contains $.ajax function.
3) Function calls (datatype = JSON) to my controller, then to model to get the record associated with that ID
4) The response is returned and the values are then set to their corresponding fields within my update form...including my dropdown box selection.
5) My view then pops open my update form as a dialog box with the values set. As this box is rendered, I'm then creating/populating the dropdown box. This is where/when I want the drop box to be set to the selected value.

One other thing that I've noticed is that currently if I update a record and set a dropdown to a value (i.e.-Washington, DC) and save, then go into another record to update it, visually the dropdown is set to the value of what I selected in the previous record (Washington, DC), but the record in my database is set to the correct value (Los Angeles)...not sure if this is a mutually exclusive issue or not. Thanks!
#14

[eluser]Aken[/eluser]
Okay I see what you're trying to do. First, let me help you out a little by being more specific on the steps that are taken:

1) The initial page is loaded. Your blank update form is generated, but hidden.
2) When an "update" link is clicked, an ajax request is sent to get the current details of that record ID.
3) jQuery takes that JSON response and populates the blank form with values.
4) jQuery shows the blank form as a popup (modal) window.

Now then, I guess you have two options (IMO).

1) When your page initially loads with your blank form, your select drop box already has all available options inside of it. This is my recommended solution if your drop down does not need to change its available options depending on ID. Then, your JSON response for the drop down will contain the value of the <option> you want to be selected. Then just use .val(response.dropdown) or whatever is appropriate.

2) If you want to update the available options in the drop down every time update is chosen, then you'll need to have another item in the JSON response, most likely an associative array containing the name and value of each option. Then loop through it to generate the HTML, update the HTML, then set the value.
#15

[eluser]theprodigy[/eluser]
another less accepted way of handling it, but may be easier would be to return the html of the form inside the json object (json.html would hold the html of the form). You could then load a view in your controller, passing in everything you wanted to set, and use php to set everything rather than javascript (depending on your level of comfort with javascript/jQuery).

As I said, it is less accepted, but is a solution. All you would have to do in javascript is set the html of your modal window to the html that gets returned, then pop it open.




Theme © iAndrew 2016 - Forum software by © MyBB