Hi, I'm using Remy Sharp's auto populate select box plugin and everything is working fine apart from one small detail. I basically have a form where a user selects an option via a dropdown select box, then (using AJAX and JQuery) the values in a secondary select dropdown box are populated based on the selection made. The user can then select an option from the secondary dropdown box then submit the form. I process the values in PHP and return a set of results and when I display the results I want the dropdown boxes to be populated with the values which the user selected when submitting the form. This is where the problem lies, I can't seem to get the secondary dropdown box to show the posted option as "selected", instead it just defaults to the first item in the list.
I have the posted id which I want to use as the selected value in the secondary dropdown, but I'm just sure how to interact with the plugin to pass this value from my php code. If anyone can help. that would be great as i'm stumped here.
Plugin code (tweaked a bit)
Code JavaScript:(function ($) { $.fn.selectChain = function (options) { var defaults = { key: "id", value: "label" }; var settings = $.extend({}, defaults, options); if (!(settings.target instanceof $)) settings.target = $(settings.target); return this.each(function () { var $$ = $(this); $$.change(function () { var data = null; if (typeof settings.data == 'string') { data = settings.data + '&' + this.name + '=' + $$.val(); } else if (typeof settings.data == 'object') { data = settings.data; data[this.name] = $$.val(); } settings.target.empty(); $.ajax({ url: settings.url, data: data, type: (settings.type || 'get'), dataType: 'json', success: function (j) { var options = [], i = 0, o = null; for (i = 0; i < j.length; i++) { // required to get around IE bug ([url]http://support.microsoft.com/?scid=kb%3Ben-us%3B276228[/url]) o = document.createElement("OPTION"); o.value = typeof j[i] == 'object' ? j[i][settings.key] : j[i]; //wee hack here as i want to concatenate the job number and description o.text = typeof j[i] == 'object' ? j[i]['id'] + ' :: ' + j[i]['description'] : 'There are currently no jobs for this client'; //original line below //o.text = typeof j[i] == 'object' ? j[i][settings.value] : j[i]; settings.target.get(0).options[i] = o; } // hand control back to browser for a moment setTimeout(function () { settings.target .find('option:first') .attr('selected', 'selected') .parent('select') .trigger('change'); }, 0); }, error: function (xhr, desc, er) { // add whatever debug you want here. console.log(desc); } }); }); }); }; })(jQuery);



I will just add an isset($_POST) test to this and hopefully this should do the trick

Bookmarks