Quantcast
Channel: Topic Tag: MySQL | WordPress.org
Viewing all articles
Browse latest Browse all 5534

bcworkz on "Dynamically Add Dropdown Values"

$
0
0

Hi Manny,

If the amount of secondary data is not huge, you could simply place all possible variations in a dynamically generated array when the page is output, then when the first selection occurs, pick the related data out of the array and populate the second drop down. Simple javascript not too different than the conditional textbox problem.

Obviously there comes a point where that becomes too cumbersome. At that point you could submit a new page request via javascript where the returned page has the user's selection from the first drop down pre-selected and the second has the related data in it. Of course if the user changes the first selection again, you have to do a request all over again.

Not to mention full page loads just because a drop down changes is kind of ridiculous and a bad user experience. Which brings us to AJAX. This is how this sort of thing should be handled. Instead of loading the whole page again, we only load the part that changes. There's a lot of info about on how to do AJAX on the Internet, but most of them do not address the technique in WP, which has some critical differences. One reference you can use is here: https://developer.wordpress.org/plugins/javascript/

The link is just to a landing page, the next 4 pages describe the entire process. While it's possible to do AJAX with javascript alone, it's rather clunky. You really would be better off using jQuery. Using jQuery means you should no longer simply output script blocks on the page, you should enqueue your script using wp_enqueue_script().

All of this is a lot to digest. You've demonstrated good coding aptitude with the conditional textbox problem, you can do this. You will find in the end the resulting code is not that extensive. It's just a lot of concepts to grasp.

I would suggest you first create some very basic test code for each small element so that the possible problems you might introduce is limited. Incrementally add more functionality, constantly testing. Once you get full round trip data between browser and server, only then should you introduce your actual data.

As always, if you get stuck somewhere, we're here to help, good luck!

BC


Viewing all articles
Browse latest Browse all 5534

Trending Articles