471,354 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

passing retirned value from Javascript to PHP

Hi,
how can I pass the returned value from Javascript to PHP?
I have:

------------------------------------------------------------------------
------

if ( x>y) {
echo '<script language="JavaScript">';
echo 'confirm(\''.$itemname.' requires '.$optionname.' Do you want
to add '.$optionname.'?\')</script>';

<I want to return the true/false value returned when user presses
OK/Cancel, to PHP code>

}

------------------------------------------------------------------------
------

Thanx
Jul 17 '05 #1
2 3373
On Thu, 11 Mar 2004 14:04:42 GMT, "Xerxes" <as*******@hotmail.com> wrote:
how can I pass the returned value from Javascript to PHP?
I have:

------------------------------------------------------------------------
------

if ( x>y) {
echo '<script language="JavaScript">';
echo 'confirm(\''.$itemname.' requires '.$optionname.' Do you want
to add '.$optionname.'?\')</script>';

<I want to return the true/false value returned when user presses
OK/Cancel, to PHP code>

}

------------------------------------------------------------------------
------


Is this triggered by submitting a form? If so, use Javascript to modify the
form to introduce an <input type="hidden"> element, or set the value of an
existing <input> element, so it's available to the PHP script it's submitted
to.

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.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
<http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space>
Jul 17 '05 #2
> >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

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by domeceo | last post: by
9 posts views Thread by Max | last post: by
5 posts views Thread by Steve | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.