TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   passing a java variable to php (http://www.talkphp.com/advanced-php-programming/4098-passing-java-variable-php.html)

pipesportugal 04-04-2009 05:03 PM

passing a java variable to php
 
Hello dear colleagues from the TalkPHP forum,

I often use the "alert" java function in the middle of PHP programs in order to display variables on screen as shown below.

PHP Code:

echo("<script>window.alert('var= ".$var."')</script>"); 

Now I wanted to use the "confirm" javascript function inside of a "for" cycle, so that if I click the OK the cycle should keep going on otherwise if I would click on the cancel there would be an exit from the "for" cycle (like the php break; function).

PHP Code:

echo("<script>window.confirm('var= ".$var."')</script>"); 

Can someone tell me how I can achieve this ?

Thanks in advance to all the help,
pipesportugal

Sakakuchi 04-04-2009 05:39 PM

1. What you are looking for should be coded in Javascript and not PHP, since the code runs on the client and not the server. For this just make the prompt-box calling a function which calls up the prompt-box again (when clicked on ok).
2. Anyhow, you could do it with PHP (and Javascript). Simply call the site again when clicking on ok - and just give a $_GET variable with. Then make php writing the confirm box again. So it would pop up untill the user klicks on cancel. But thats some dump and useless way to do it ;-)
3. Java != javascript

Tanax 04-04-2009 07:24 PM

That's not Java.
That's Javascript. Totally different.

jcorradino 04-05-2009 06:27 AM

as said above, JavaScript ≠ Java in any way shape or form.

Anyway, the only way to pass any user information (JavaScript data is considered user data in this case, since all JavaScript is processed client side, or on the browser) to the server is through either a page reload with the content in a $_GET variable, or through an xmlHTTPrequest

The first solution
Code:

<script type="text/javascript" languge="JavaScript">
window.location.href = "yourwebpage.php?variable=passedData";
</script>

From there, you can simply grab the variable in PHP through $_GET["variable"] (after being properly sterilized, of course)

The second solution is a little more in depth, but will look better. It will open a live connection with the processing page, send the information, and return the data... all without reloading your current page. This means it will be a real-time change to the user, making less click though and a (normally) more pleasurable visit to your site.

Code:

<script type="text/javascript" charset="utf-8">
  var response=null;
  var param="your passed data"
  function ajaxRequest(param) {
    var request = false;
    if (window.XMLHttpRequest) {
      request = new XMLHttpRequest();
      if (request.overrideMimeType) {
        request.overrideMimeType('text/xml');
      }
    } else if (window.ActiveXObject) {
      try {request = new ActiveXObject("Msxml2.XMLHTTP");}
      catch (e) {
        try {request = new ActiveXObject("Microsoft.XMLHTTP");}
        catch (e) {}
      }
    }
    if (!request) {
      alert('There was an error in creating an XMLHTTP instance, please update your browser!');
      return false;
    } else {
      request.onreadystatechange = function() { statusCheck(request); };
      request.open('POST', 'yourProcessingPage.php', false);
      request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        request.setRequestHeader("Content-length", param.length);
        request.setRequestHeader("Connection", "close");
        request.send(param);
    }
  }
  function statusCheck(request) {
    if (request.readyState == 4) {
      if (request.status == 200) {
        window.response=request.responseText;
      } else {
        alert('There was a problem with the request.');
      }
    }
  }
</script>

If you choose to go this way, it will send as $_POST, so adjust accordingly. If you want to show a response somewhere on the page, just call a function at the end of statusCheck that will display window.response.

This should get you started, there is a lot of documentation online about this stuff, and finding information on your own can help learn this stuff faster. Good Luck!

pipesportugal 06-07-2009 10:32 AM

Hi everyone,

Thank You to those that gave me their positive contribution.

I acknowledge the fact that java is different from javascript, thank You all for that.

Regarding Jason's suggestion, I dare at this point to ask him for a short "line by line" explanation of the example code that was posted here.

Thanks in advance,
pipesportugal

Enfernikus 06-07-2009 04:03 PM

Jason's suggestion if an example of AJAX

Code:

var request = false;
    if (window.XMLHttpRequest) {
      request = new XMLHttpRequest();
      if (request.overrideMimeType) {
        request.overrideMimeType('text/xml');
      }
    } else if (window.ActiveXObject) {
      try {request = new ActiveXObject("Msxml2.XMLHTTP");}
      catch (e) {
        try {request = new ActiveXObject("Microsoft.XMLHTTP");}
        catch (e) {}
      }
    }

This chunk simply attempts to create a new XMLHttpRequest object which is use for asynchronous request.

Code:

if (!request) {
      alert('There was an error in creating an XMLHTTP instance, please update your browser!');
      return false;
    } else {
      request.onreadystatechange = function() { statusCheck(request); };
      request.open('POST', 'yourProcessingPage.php', false);
      request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        request.setRequestHeader("Content-length", param.length);
        request.setRequestHeader("Connection", "close");
        request.send(param);
    }
  }


This chunk does the work. Setting proper headers and sending the actual request. From the onreadystate property we can see that statusCheck has been set to verify results.

Code:

function statusCheck(request) {
    if (request.readyState == 4) {
      if (request.status == 200) {
        window.response=request.responseText;
      } else {
        alert('There was a problem with the request.');
      }
    }
  }


This is statusCheck function which was set to check response and verify all went well if it did than use the returned data and if not alert the user.

Orc 06-07-2009 04:06 PM

Aww man, I came in here to find javascript, not java. Would have been interesting to see what this could have been. :-(

jcorradino 06-07-2009 04:08 PM

HTML Code:

<script type="text/javascript" charset="utf-8">
  var response=null;
        //This is to be the data you are passing through the AJAX call, replace "your passed data"
  var param="your passed data"
        //This function will actually make the AJAX call
  function ajaxRequest(param) {
    var request = false;
        //Checks to see if a request can be made (non MS browser)
    if (window.XMLHttpRequest) {
      request = new XMLHttpRequest();
      if (request.overrideMimeType) {
        request.overrideMimeType('text/xml');
      }
        //Checks to see if a request can be made (MS browser)
    } else if (window.ActiveXObject) {
      try {request = new ActiveXObject("Msxml2.XMLHTTP");}
      catch (e) {
        try {request = new ActiveXObject("Microsoft.XMLHTTP");}
        catch (e) {}
      }
    }
        //Checks to see if the request from above was able to be made, if not, will display error
    if (!request) {
      alert('There was an error in creating an XMLHTTP instance, please update your browser!');
      return false;
    } else {
          //Checks to see if there is any returned status
      request.onreadystatechange = function() { statusCheck(request); };
          //Opens a connection with another page, using (in this case): Post instead of Get, the page yourProcessingPage.php, and false meaning it will stall the function to wait for the response, true rarely works
      request.open('POST', 'yourProcessingPage.php', false);
      request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        request.setRequestHeader("Content-length", param.length);
        request.setRequestHeader("Connection", "close");
                //Sends data
        request.send(param);
    }
  }
  //checks the status of the request
  function statusCheck(request) {
        //Check if the request is complete
    if (request.readyState == 4) {
          //HTTP status of 200 means ok (500 means server error and 404 means page not found)
      if (request.status == 200) {
                //Sets response text to javascript variable window.response
        window.response=request.responseText;
          //something fubared, displays error
      } else {
        alert('There was a problem with the request.');
      }
    }
  }
</script>

hope this helped :)

Wildhoney 06-07-2009 04:19 PM

You want to pass a JavaScript variable to PHP?

pipesportugal 08-27-2009 07:08 PM

Hi everyone,

I will rephrase my question.
I normally use the javascript function window.alert to stop the system and show me some variable values, this is a kind of my personal debugger.
It stops, and then I look at the variabe value then click on OK and the program goes on...

If I use the window.confirm will show me the OK and CANCEL buttons.

After clcking the OK button the program will go on, and if I click the CANCEL button the program does the same.

How can I find out on my php program, of which button has been clicked ?

Thanks in advance for all the help,
pipesportugal

jcorradino 08-27-2009 08:42 PM

If you are looking to do what I think you want to do, you cant. Once the javascript is running, the php is already completed its job, and cannot be accessed again unless you use an AJAX call or refresh the page..

pipesportugal 08-27-2009 10:49 PM

ok thank You so much Jason.
pipesportugal


All times are GMT. The time now is 10:13 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0