> >how can I pass the returned value from Javascript to PHP?
I have:
Since the only way to get something run by a client is to post it to
another PHP script, it can't be done 'inline', so it's off-topic here, try a
Javascript group. Followups set to comp.lang.javascript.
Actually, if you are determined to do this, it CAN be done in PHP, and so it
is on topic. It is just rather convolulted.
Here is the outline. Assume that you have a form (or something). When you
change the value in something, like a drop down box, you would like to
change the values of another drop down box, BUT, you want to run server side
code to accomplish this. I use this technique, a lot, to perform dynamic
drop down box generation, ON THE SAME screen, fom the server, WITHOUT a
refresh.
On the page you want this to happen:
Include a form with the drop down box that the user can select from. This
drop down box has an "onChange" event attached to it. One I use looks like
this:
function doDB () {
document.input.tbl.disabled = false;
if (needClear) {
removeAll(document.input.tbl);
document.input.tbl.disabled = true;
}
var theElement = document.input.db;
var theIndex = theElement.selectedIndex;
var theValue = theElement.options[theIndex].value;
removeAll(document.input.tbl);
addOption (tbl', 'Select', 'NONE');
top.hidden.document.location =
"../include/loaders/gettables.php?thedb=" + theValue;
}
Here is the other routine that are used, just for your reference:
function removeAll(theElement) {
var selectLen = theElement.length;
for (var i = 0; selectLen > i; i++) {
removeLast(theElement);
}
}
I use a boolean called needClear that determines if I should erase the
contents (based on other form elements that the user might check)
(the routine gettables.php is the one that does all the server side stuff.
You will need to write your own that does what you want, so I will not
include it. However, you also need this routine to return the values: this
is the strip of code that does that. I assume that you are capable of
putting something into $tables <grin>:
echo "<script>\n";
foreach ($tables as $key => $value) {
echo "parent.addOption ('tbl', '" . $value . "', '" . $key . "');";
}
echo "</script>\n";
Include another drop down box (preferably inactive for the user's sake)
where the dynamic values will go.
Include a blank inline frame, hidden, somewhere on the page. The one I use
looks like this:
echo "<div id=\"iframe\" style=\"display:none;\">\n";
echo "<iframe src=\"../blank.php\" name=\"hidden\"></iframe>\n";
The source for blank.php looks like this:
<htm>
<head>
<title></title>
</head>
<body>
</body>
</html>
(spiffy, eh?)
That should get your going. Dynamic population of drop down list boxes,
WITHOUT a refresh, and using client-side data. I lifted these routines and
adapated them from various places across the internet. I just don't like the
word "impossible" <grin>
Enjoy!
david