Can anyone please give me an example of passing information FROM java to PHP and vise versa.

6 Years
Discussion Span
Last Post by jkon

Now when you say Java, do you mean Javascript or the actual Java language? I don't know diddly about the actual language but if you mean javascript, read on.

Passing from PHP to Java isn't terribly difficult.

echo "<script type='text/javascript'>myJSFunction('$phpvariable')</script>";

Passing the other direction poses problems because PHP is a pre-processed script, so PHP has done it's job before the page ever gets sent to your browser. Javascript then runs in your browser after the page gets sent and does it's thing.

There is a mighty simple way to pass stuff back to PHP but it involves a page reload.

*Note: this code doesn't work, it's just an example

How old are you?
<input type='text' id='thatold' name='thatold' onchange='myJSFunction(this)' />
function myJSFunction(what)

The location.replace sends the browser to the web address with the $_GET variable 'age' set to whatever the contents of the input box are.

Then you'd retrieve it from return.php with a $value=$_GET statement.

Now having said that, there is another way that is not so simple but doesn't require reloading the page called xmlHTTPRequest.

I snipped some of the code from my website so you could get an idea if it's something you want to pursue.

This part, along with another function called useHttpResponse(which is super secret) is placed in my normal website page.

// the parameter is a kind of key, so I can't show it to you.

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  httpobj=new XMLHttpRequest();
  {// code for IE6, IE5
  httpobj=new ActiveXObject("Microsoft.XMLHTTP");

function updateData(param) {
  var myurl = "xmlsvcs.php?cmd=";
  var myRandom = parseInt(Math.random()*99999999); // cache buster  

	httpobj.open("GET", myurl + escape(param) + "&rand=" + myRandom, true);
  httpobj.onreadystatechange = useHttpResponse;

function useHttpResponse() {
  if (httpobj.readyState == 4) {
    var textout = httpobj.responseText;
    // the variable 'textout' contains the information returned from the webpage

Then I have a file called xmlsvcs.php on my website who's sole duty is to process xmlhttprequests.

if ($_GET['cmd'])
 switch ($command)
	case "*********":  
	 echo "Information to return to the web page already loaded.";
	 echo "0"; 

Once again, this is a code snippet and won't work standalone. The part that's most important is the echo "yada yada" statement. That's one way you can return data to the javascript function on the browser. The other way is to encode it as xml.

Anyway, hope that helps a little.



If you mean JAVA there are many ways Web Services, cookies (if in same domain) , session (if in same application server) even EJB (of course in that case you would need to create the PHP parser) and many-many more ..
Also let me express that as JavaScript-PHP communication dietdew12z answer is really good giving the basics.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.