Mark
|
| Posted: 06/26/2002, 1:29 PM |
|
Hi,
What I'm trying to do is to get one listbox populated based on another.
I know there's a javascript workaround for this, but I don't want to load all the data to the client, as I'm acutely aware how easy it is for your PC to run out of resources, regardless of the RAM available !
How can I make use of a button or link when clicked, to force the listbox in question to re-populate itself from the server ?
For Example, I think what I could do is set a session parameter in the Client OnChange event of the first listbox to hold the new key based on the selection made.
Then have a button which has a server OnClick event, that executes some SQL based on the session parameter to get the correct entries & then populate the second listbox....
That's my theory, but can anyone show me how to actually do this as I'm not having any luck so far !!
I'm a beginner at all this, so a patient explanation would be appreciated !
Using PHP & MySQL.
Many Thanks,
Mark
|
|
|
 |
Nicole
|
| Posted: 06/27/2002, 11:22 PM |
|
Mark,
I believe that the workaround is easier.
Assuming that second listbox is populated depending on the item that is selected in the first list. So you can create small JS function that will be assigned on listbox1 onchange event. This function just should redirect user to the same page but with the parameter string. That, in its turn, should contain the name and value of selected item from listbox1. So when page is reloaded sql for listbox2 is executed with Where clause.
To do it create custom sql like:
ASP
select primary_key, showing_field from table name " & where &"
PHP
select primary_key, showing_field from table name " . $where ."
Then create form Open event and build Where clause depending on passed parameter.
|
|
|
 |
Labs4.com
|
| Posted: 06/28/2002, 2:38 AM |
|
button for populating second listbox is probably the best way.
I don't recommend using onChange with javascript window.locaton.href because many people are using scrolling wheel on their mouse to scroll down the form and if you have blurred listbox you will accidentaly refresh your page, this sucks especially if listboxes are not on the top of the form but somewhere in the middle and you already filled-in bunch of stuff.
I should use probably DOM functions to clean-up/re-fill second listbox upon click on [>>] button, well this is the last easy imaginable part.
Now the more tricky part:
We have form <FORM NAME="form" ACTION="something.php" METHOD="POST">
We have listbox1
Somewhere on page create <IFRAME name="processor" width=100 height=0 border=0><IFRAME>
[>>] button use <input type="button" value=">>" onClick="processor.location.href = 'populate.php?submenu='+ document.form.listbox1.options[document.form.listbox1.selectedIndex].value; return false;">
now create file populate.php and there
- first clean-up listbox2 in main form by (remember that you must do it, because people will be re-populating several times)
<SCRIPT LANGUAGE=Javascript>
parent.form.listbox2.options.length = 0;
</SCRIPT>
- second populate second listbox values from database and put them into array, make loop and assign one by one to listbox2 by javascript function
in basic PHP it should look like
<?
$query = mysql_query("SELECT text,value FROM list_of_stuff WHERE condition=$submenu ORDER BY text",$db);
while($result = mysql_fetch_array($query)) {
echo "<SCRIPT LANGUAGE=Javascript>"
echo "var opt = new Option('".$result[text]."', '".$result[value]."');";
echo "parent.form.listbox2.options[sel.options.length] = opt;";
echo "</SCRIPT>";
}
?>
and that's it, listbox2 is populated with new values according to selection in listbox1 without refreshing form itself.
This was just theoretical guide HOW TO, I didn't test it so there can by typos or bugs but I'm afraid this is nothing for beginners either.
Good Luck
Josef
|
|
|
 |
Mark
|
| Posted: 07/02/2002, 5:18 AM |
|
I'll give it a try !
Regards,
Mark
|
|
|
 |
Robert Rodgers
|
| Posted: 07/02/2002, 7:08 AM |
|
Mark
Another way is to keep posting to the current page. Here is link to an example. This is an ASP example but I don't think it should matter as I did not use any vbscript in the example it is all just regular html forms and ccs functions.
You will need to set up a connection named "Connection1" that points to the
Internet database (this example uses "users", and "user_groups") and point
to your publishing location
--
Robert Rodgers
Robert@SylvanComputing.com
http://www.sylvancomputing.com/codecharge/linkedlist.zip
|
|
|
 |
|