-1

Possible Duplicate:
How to pass JavaScript variables to PHP?
send javaScript variable to php variable

I have document.getElementById('name') field in a document that it's value is changed by script, when i do a post(to server) i want to get the current value of the document.getElementById('name') and set it in

in

$temp= document.getElementById('name');
?>

p.s. I know that the script code is don client side and the php i server side but i need this workaround so please help..

Community
  • 1
  • 1
Tony
  • 1,175
  • 4
  • 14
  • 22

2 Answers2

2

I think the best option you have is to make an AJAX request with the values. With this you can send and assign the values to your PHP variables.

Else you can use COOKIE etc. to store the value temporarily and then use it on server side whenever a server side function is called next.

Ajax code

<script type='text/javascript'>
    function getXMLHTTPRequest() {
    try {
    req = new XMLHttpRequest();
    } catch(err1) {
      try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (err2) {
        try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (err3) {
          req = false;
        }
      }
    }
    return req;
    }



      var http = getXMLHTTPRequest();
       function sendValue() {
    var myurl = "session.php";  // to be present in the same folder
     var myurl1 = myurl;
    var value = document.getElementById('urDIV').value;
      myRand = parseInt(Math.random()*999999999999999);
      var modurl = myurl1+"?rand="+myRand+"url"+value ; // this will set the url to be sent

      http.open("GET", modurl, true);
      http.onreadystatechange = useHttpResponse;
      http.send(null);
    }


    function useHttpResponse() {
       if (http.readyState == 4) {
        if(http.status == 200) {
          var mytext = http.responseText;
          // I dont think u will like to do any thing with the response
          // Once you get the response, you are done.
            }
         }
         else {
             // don't do anything until any result is obtained
            }
        } 
</script>

HTML Part

// considering you are calling on a button call

<input type='button' onclick='sendValue();'>
Prashant Singh
  • 3,725
  • 12
  • 62
  • 106
  • Any referals to any examples how to do this ? Iam new to ajax! – Tony Oct 10 '12 at 12:08
  • Refer to this answer of mine. It contains the code http://stackoverflow.com/questions/8615348/session-variables-sending-specific-variable-from-a-while-loop – Prashant Singh Oct 10 '12 at 12:09
0

Make a new HTTP request that includes the data.

  • You could generate a <form> and submit it.
  • You could use Ajax
  • You could set location to a new value
  • etc
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335