Honesty Rocks! truth rules.

Php To Java

HOME      >>       Programming

khalilov

How do you pass a variable from PHP to JAVA?
i tried

var x= <?php echo $variable; ?>
it doesn't work

darasen

You are going to have a problem with this, obviously, given that PHP and JAVA are running on two different computers. Remeber that PHP is running on the web server and by the time a web page starts loading on the local client (or web site visitor) machine PHP is already done and no longer executing. Then once the page is loaded for the visitor any Java script or Java applet can run. Thus the best method for the two to share data seems to be the web page and/or the HTML created by PHP. Perhaps trying to have a form element (hidden if you wish) with a value written in by the PHP and then once the Java starts to execute have Java read that value into a variable. There may be other ways as well but this is off the top my head and sems to be a fairly simple solution.


khalilov

Hmm... what i want to pass is a number form database, lets say i got this number and its $x, how do i pass it?


pyost

The code you wrote should work, I'm sure something else is the real problem :mellow:


docduke

Do "View | Page Source" in your browser, and see what actually arrived in the web page. If $variable had the value 5 in the database accessed by the PHP script, the page source should have var x=5 when it arrives in your browser.

 

If it does not, then you can localize the cause. For example, if the <?php string is still there, you aren't executing the PHP. If the 5 is there but the browser-side result isn't what you want, then you may be generating a web page with a syntax error, such as a missing semicolon.

 

If necessary, add some print statements in the PHP that generate plain HTML, so you can debug what is actually happening, as opposed to what you intended.

 

Computers have the bad habit of doing what you tell them to do, not what you want them to do! :mellow:


khalilov

Hmm.. i figured out part of the problem

<html><body><script type="text/javascript"><?php$string="5";echo "var my_var ='".$string."'"; // I should have added the "'" for some reason?>document.write(my_var)</script></body></html>

However when i put this:
<html><body><script type="text/javascript"><?php $x=5;echo "var x='".$x."'";$string="5";echo "var my_var ='".$string."'";?>document.write(x)document.write(my_var)</script></body></html>
It seems when i start operating with two variables, nothing shows.
Page source
<html><body><script type="text/javascript">var x='5'<br>var my_var ='5'document.write(x)document.write(my_var)</script></body></html>
I only need to pass one variable atm, but in the future i might need to pass out more, anyone can tell me why having more than one variable causes issues?