View Single Post
Old 04-05-2009, 06:27 AM   #4 (permalink)
jcorradino
The Contributor
 
jcorradino's Avatar
 
Join Date: Sep 2008
Posts: 36
Thanks: 2
jcorradino is on a distinguished road
Default

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!
__________________
Jason Corradino
Applications Developer, Interactive Support - Tribune Technology
J2EE Development, Script Tinkering - Develop, Support, and Maintain Tribune websites.
jcorradino is offline  
Reply With Quote
The Following User Says Thank You to jcorradino For This Useful Post:
pipesportugal (06-07-2009)