Page 1 of 1
Dynamic List generation
Posted: Mon Dec 12, 2005 10:01 am
by nawhaley
Hey guys I'm trying to get a list to change dynamically while running a PhP application. What I mean by this is I have a store chain list that I want to filter the store name list down to just those store's related to that chain once its selected. I figure I'm going to have to use some javascript like an onchange function to tell when the lists focus has changed but how do I get that variable over to the other list and update the page without losing all the users data for one and repopulating the other list.
Posted: Mon Dec 12, 2005 10:09 am
by RobertGonzalez
Usually, when using JS with something like this, you build two lists. One that has the selectable items the user can choose and another that is populated by the JS script depending on what the user chose. When combining that with PHP you are probably going to want to populate the first select list with the appropriate output from your DB or Array, then maybe include some of that into your second select list.
There is a killer section on JavaScript at the
CodingForums. You might want to check there also.
Posted: Mon Dec 12, 2005 10:15 am
by nawhaley
thats correct each store is linked to a chain in my relational database so I pull the orginal lists values from the database using PhP and need to change the second list (store list) according to the value in the orginal list by only pulling values from the database that match the selected chain. Now I can populate the list for the chain what I cannot figure out is how to get that value stored so the next list can be populated using only values from the chain. I suppose I could make an intermediate page and just repopulate each value based on POST data but it seems like a hassle to make a whole new page if I can avoid it.
Posted: Mon Dec 12, 2005 10:21 am
by pickle
If you want to change a drop down list dynamically, you've got two options (as far as I know):
- Generate the code for all possible chain lists at run-time, then when the user chooses the chain dynamically load in the appropriate drop down box.
- Use xmlhttp to make Javascript call a server-side PHP script which generates & returns the appropriate drop down box, then Javascript can load it in.
Posted: Mon Dec 12, 2005 10:26 am
by nawhaley
the first ones not really feasible since I dont know how many chains and stores per chain were going to have so the latter is most likely the one I need to do. Does anyone have any referances on XML so I can research this some more I mostly know a little javascript and some PhP havent had a need to pull in XML yet.
Posted: Mon Dec 12, 2005 11:02 am
by pickle
Probably the best tutorial you can get is here at Devnet:
viewtopic.php?t=34665&highlight=xmlhttp+tutorial
Posted: Mon Dec 12, 2005 1:09 pm
by nawhaley
I looked at that and while its somewhat helpful it still leaves a heck of alot to be desire like here are a few questions that no matter how many times I read the code I cant find an answer to.
-is the file saved as .xml file extension if not it would be normal html and the whole thing would expode because you cant put functions in normal HTML it would just print them as text.
- Where is updateIt() being called from it looks like a useless function nothing ever refers to it so regardless of what language your using its going to skip it till called.
-Even though its assumed that $_SESSION['loggedin'] and $_SESSION['loggedinid'] are already initialized where were they initialized how did he get the post data from them without leaving the page?
-What the heck is $_POST['upd'] I assume its some sort of button or something on the page? Something to tell the page to update without actually refreshing?
There is alot of stuff just assumed in this code to much for me to use as a learning tool for XMLhttp and do anything but spend a week pulling errors out of the code so can anyone explain this in more detail so maybe I can derive a solution for what I'm trying to do based off it?
Posted: Mon Dec 12, 2005 2:13 pm
by pickle
Here's an example implementation (in pseudo-code)
Code: Select all
<html>
<head>
<script type = "text/javascript">
function loadList(passed_value)
{
set_up_xmlhttp_code()
var xmlhttpresults = send_xmlhttp_code(passed_value);
replace(particular_dropdown,xmlhttpresults);
}
</script>
</head>
<body>
<select onChange = "loadList(this.value);">
<option>1</option>
<option>2</option>
</select>
</body>
</html>
send_xmlhttp_code() would reference a PHP file on the server: myList.php for example:
Code: Select all
myList.php:
<?PHP
$passed_value = $_GET['passed_value'];
switch($passed_value)
{
case 1:
//echo some stuff
case 2:
//echo some other stuff
}
?>
The javascript variable xmlhttpresults could then be populated with whatever myList.php outputs.
Posted: Mon Dec 12, 2005 2:18 pm
by nawhaley
so your telling me that basically I have to take a parameter sent through the URL pull that out and search my database. Then print that out on the php page as something along the lines of a comma delimited list so javascript can parse it into a list?
Posted: Mon Dec 12, 2005 2:26 pm
by pickle
nawhaley wrote:so your telling me that basically I have to take a parameter sent through the URL pull that out and search my database.
Yes.
nawhaley wrote:Then print that out on the php page as something along the lines of a comma delimited list so javascript can parse it into a list?
No.
You can make that PHP page just echo an html drop down box. As it is echoed, the xmlhttp listener will grab that output and store it in a string. You can then use Javascript to substitute that string (which is actually just xhtml code for a drop down box) into the page where appropriate.