-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..

3
  • Many dups; stackoverflow.com/questions/8191124/… & see the "Related" sidebar Commented Oct 10, 2012 at 12:07
  • Is there any particular reason why you don't just post the name? Commented Oct 10, 2012 at 12:11
  • 1
    I cant find from where, the control is <input readonly= and initial value=0 and it allways takes 0 Commented Oct 10, 2012 at 12:14

2 Answers 2

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();'>
Sign up to request clarification or add additional context in comments.

2 Comments

Any referals to any examples how to do this ? Iam new to ajax!
Refer to this answer of mine. It contains the code stackoverflow.com/questions/8615348/…
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.