Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting javascript return value

Status
Not open for further replies.

bardley

Programmer
May 8, 2001
121
US
Hi. We're trying to run some javascript in a page and have PHP pick up the return value. It seems like this isn't going to work if we do
<script>
</script>
some html
<? blah ?>

But is it possible to run the javascript from inside PHP tags, and capture the return value in a variable?

ANY advice is extremely helpful. Thanx!

Here is the code, and we need to get the value of gUser:

<SCRIPT LANGUAGE=JavaScript>
<!--
var gUser;
function doOnLoad() {
gUser = window.dialogArguments;
// document.writeln(&quot;User: &quot; + gUser);
}
function doOnChange() {
document.all[&quot;btnSubmit&quot;].enable();
}


// -->
</SCRIPT>
Brad Gunsalus
Cymtec Systems, Inc.
bgunsalus@cymtec.com
 
No,

you can send variables from PHP to JavaScript, but not the other way around. PHP is server-side, JS client-side. This means that PHP is processed first, then JS. This means that it's easy to send a variable to your javascript, but the other way around is impossible, since PHP is already processed before the JS is &quot;activated&quot;.
You can send the value of gUser to the page with JS, letting PHP process the var on a second page load. Use the

location.href=&quot;<?php print(&quot;$PHP_SELF?gUser=&quot;)?>&quot; + gUser;

function (note that the PHP part is processed first, so when you load the page, it has the JS code like this: location.href=&quot;somePage.php?gUser=something&quot;)
In the same page, add a PHP fragment that checks if the gUser var is set -> if(isset($gUser))
and process the variable then.

Hope this is of some clear assistance... :)
 
Hi,

I have read the posting that rycamor provided, and although it does work, it is such a 'hack' that advanced use of this method will result in a large number of headaches.

If you will provide an exact sample of what you are looking to do, I am sure we can come up with a solution for you without any problems.
 
Well islandpac, you and I know that the only way to get any sort of return variable in PHP is for the browser to request something. If you want the browser to run some Javascript, and then give another variable to PHP while loading the current page, there is only one other way to do it: use a frame somewhere, or an IFRAME, if you can get away with it. The frame method would bring with it at least as many headaches, if not more.

(Or for a simple clunky method, I suppose you could just load the page, then when you get the Javascript variable, direct Javascript to reload the page, with the added variable on the querystring.)

Any other method would require the use of at least some Java or ActiveX (which I am not at all against: if you really want to do anything advanced in this area then Java or ActiveX is the true way to go).

Failing that, my method is yes, a hack, but it will work, and it uses very basic established Javascript routines (document.write). With the problem of returning variables to PHP, it does take a little work to escape strings or serialize arrays, but that just goes with the territory.

I'm curious, though; if you have any other solutions, I am fully willing to re-evaluate my opinions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top