Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


Jeeves

301 posts

Ultimate Geek


#193455 11-Mar-2016 16:31
Send private message

So I have an ajax script for cascading drop down forms.

 

At the moment it's called when the "master" drop down is changed or focused, using the jquery on() function. When the user selects an item from the master dropdown, the ajax script is called to load a php script that checks the sql DB for all the "children" of that master, which then populates the child drop down list.

 

 

 

$("select#master").on("change focus", function(){
var master = $("select#master option:selected").attr('value');
if (master.length > 0 ){

$.post("inc/fetchChild.php",
{
id: master
},
function(data){
$("#child").html( data );
});

};
});

 

 

 

This works all well and good - no dramas. 

 

The problem is I want this script to fire on page load. This is because the form data at the next page is saved as session variables, so the user can go back and forth between the form stages and each form element checks to see if there is a associated session variable, and if there is, loads it up. 

 

Im doing this because the form is very long and broken into 5 sections - so I dont want the customer losing their form data if they go back and forth.

 

 

 

So, I need this above script to fire on page load because if the user has already selected the child and master previous - then php loads the "master" drop down with the user selection already loaded and selected. This once the page has finished loading, the ajax script needs to fire - sees that the master is already loaded, and subsequently loads the children for that master.

 

 

 

So going back to the main problem. If I insert "ready" into the ".on("change focus", function()" function ..... .on("change focus ready", ... it doesn't work.

 

 

 

However If I copy and past the above functions contents into the root $(document).ready(function(){ function, it works fine! Like this:

 

 

 

$(document).ready(function(){


var master = $("select#master option:selected").attr('value');
if (master.length > 0 ){

$.post("inc/fetchChild.php",
{
id: master
},
function(data){
$("#child").html( data );
});

};


$("select#master").on("change focus", function(){
var master = $("select#master option:selected").attr('value');
if (master.length > 0 ){

..........................etc...

 

 

 

 

 

Problem is now I have pretty much a duplicate function, which isn't very efficient and a but ugly.

 

I hope I'm explaining myself clear enough...

 

Can I reconfigure this on function to fire on page load as well as when the user clicks on the master list?

 

 

 

 

 

 


Create new topic

This is a filtered page: currently showing replies marked as answers. Click here to see full discussion.

geocom
594 posts

Ultimate Geek

Subscriber

  #1511472 11-Mar-2016 16:37
Send private message

You would put everything in the function into its own function

 

So like follows

 

function reload_list(){

 

var master = $("select#master option:selected").attr('value');
if (master.length > 0 ){

$.post("inc/fetchChild.php",
{
id: master
},
function(data){
$("#child").html( data );
});

 

}

 

$("select#master").on("change focus", function(){

 

reload_list();

 

});

 

$(document).ready(function(){

 

reload_list();

 

});





Geoff E


Create new topic





News and reviews »

Air New Zealand Starts AI adoption with OpenAI
Posted 24-Jul-2025 16:00


eero Pro 7 Review
Posted 23-Jul-2025 12:07


BeeStation Plus Review
Posted 21-Jul-2025 14:21


eero Unveils New Wi-Fi 7 Products in New Zealand
Posted 21-Jul-2025 00:01


WiZ Introduces HDMI Sync Box and other Light Devices
Posted 20-Jul-2025 17:32


RedShield Enhances DDoS and Bot Attack Protection
Posted 20-Jul-2025 17:26


Seagate Ships 30TB Drives
Posted 17-Jul-2025 11:24


Oclean AirPump A10 Water Flosser Review
Posted 13-Jul-2025 11:05


Samsung Galaxy Z Fold7: Raising the Bar for Smartphones
Posted 10-Jul-2025 02:01


Samsung Galaxy Z Flip7 Brings New Edge-To-Edge FlexWindow
Posted 10-Jul-2025 02:01


Epson Launches New AM-C550Z WorkForce Enterprise printer
Posted 9-Jul-2025 18:22


Samsung Releases Smart Monitor M9
Posted 9-Jul-2025 17:46


Nearly Half of Older Kiwis Still Write their Passwords on Paper
Posted 9-Jul-2025 08:42


D-Link 4G+ Cat6 Wi-Fi 6 DWR-933M Mobile Hotspot Review
Posted 1-Jul-2025 11:34


Oppo A5 Series Launches With New Levels of Durability
Posted 30-Jun-2025 10:15



Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.