Avatar of T Smith
T Smith
 asked on

Dynamic dropdowns using jQuery and PHP

I've reviewed the article by Julian Hansen detailing how to create linked dropdowns using JQuery and PHP. However, how would I go about this if I have the data stored in different folders located on a server?

Ex. Main Folder > Sub Folder > 3rd folder > Document

1st Dropdown list will provide a list of states (list extracted from Main Folder)

2nd Dropdown list will provide a list of cities (list extracted from Subfolder, dependent on 1st dropdown selection)

3rd Dropdown list will provide a list of counties (list extracted from 3rd folder, dependent on 2nd dropdown selection)

4th dropdown list will provide a list of word/excel/pdf documents (list extracted from document, list is dependent on 3rd dropdown selection)

The form should display as a popup form, with submit and clear buttons.

Test.html
Untitled-3.php
Untitled-2.js
JScriptHTMLPHPjQuery

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
Julian Hansen

The sample will need to change a bit. I put together another sample to demonstrate.

HTML
	Root <select name="root" id="root"></select>
	Sub1 <select name="sub1" id="sub1"></select>
	Sub2 <select name="sub2" id="sub2"></select>

Open in new window

jQuery
<script>
$(function() {
	var root = document.getElementById('root');
	var sub1 = document.getElementById('sub1');
	var sub2 = document.getElementById('sub2');
	loadDD(root, false, false);
	
	$('select').change(function() {
		if (this.id == 'root') {
			sub1.value = sub2.value = '';
			loadDD(root, sub1, false)
		}
		else if (this.id == 'sub1') {
			sub2.value = '';
			loadDD(root, sub1, sub2);
		}
	});
});
function loadDD(root, sub1, sub2)
{
	$.ajax({
		url: 't2884.php',
		data: {
			root: root.value || '',
			sub1: sub1.value || ''
		}
	}).then(function(resp) {
		if (root.value) {
			if (sub1.value) {
				sub2.innerHTML = resp;
			}
			else {
				sub1.innerHTML = resp;
			}
		}
		else {
			root.innerHTML = resp;
		}
	});
}
</script>

Open in new window

PHP
<?php
$path = 't2884/';

$root = isset($_GET['root']) ? $_GET['root'] : false;
$sub1 = isset($_GET['sub1']) ? $_GET['sub1'] : false;

if ($root) $path .= "{$root}/";
if ($sub1) $path .= "{$sub1}/";
$path .= '*';

$dirs = array_filter(glob($path), 'is_dir');
foreach($dirs as $f) {
	$file = basename($f);
	echo <<< OPTION
	<option value="{$file}">{$file}</option>
OPTION;
}

Open in new window

Working sample here
T Smith

ASKER
Thank you for your response. Do I have to list every document under the root, rather than fetching the data from the folders to display?
Julian Hansen

Not sure I understand the question? The solution works on a hierarchical folder structure.

To display files in the last drop down you would test for all parameters not false (root and sub1 in the PHP file) - if that is the case then ask for files instead of folders.

I will see if I can update the sample to do that.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
T Smith

ASKER
If you could provide a sample that would be great. Thanks again!
T Smith

ASKER
Hi - still having problems. Can you assist?
Test_v2.html
t2884.php
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
T Smith

ASKER
Thank you! Is this possible to fetch the data locally rather than fetching from the file server? If so can you provide a sample for that? Lastly, how do I open the document (word, excel, pdf, etc.) when the user clicks a button?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

You can't for local files - the security model of the browser (for obvious reasons) prevents script having access to local resources.
T Smith

ASKER
It was a pleasure working with Julian!
Julian Hansen

Thank you and, you are most welcome.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck