0

I have a problem and need a solution. I have a server sent event which gets updated to a div id everytime when there is a change in the database. Now my question is how to get the value from that div id and save it to a PHP variable.

<script>
    if(typeof(EventSource)!=="undefined")
    {
        var source=new EventSource("bookcounterevent.php");
        source.onmessage=function(event)
        {
            document.getElementById("bookcounter").innerHTML=event.data;
        };
    }
    else
    {
    document.getElementById("bookcounter").innerHTML="Please update your browser!!";
    }
    </script>
    <div id="bookcounter"></div>

The above codes update the <div id="bookcounter"></div>, I want the value which I receive at id bookcounter, I want to save it to a php variable say $book. How can that be done?

Thanks in advance.

11
  • doesn't make sense to send the data to browser, and send it right back. Get it out of the php function that sends it. Would help if you gave a better description of what you are trying to do Commented Nov 1, 2012 at 23:50
  • I dont think you can store it in a php var (on that page), because the page is already 'executed' so the php process is already finish once result is return to the browser (unless you are doing 'Long Polling'). What you could do is store in a hidden field so you can post it back to the server if need or use a session Commented Nov 1, 2012 at 23:50
  • @R.S I'm using a long polling method, so now tell me how can I move forward from there. Commented Nov 1, 2012 at 23:55
  • @user1731476 php runs on server, javascript in client. It is not clear at all what you want to be able to do with regard to "php variable" Commented Nov 2, 2012 at 0:03
  • @user1731476 What info are you trying to 'store' in this php var? Commented Nov 2, 2012 at 0:08

1 Answer 1

1
<style>
#notification {
    color:green;
}
#nonotification {
    color:red;
}
</style>
<script> 
      if(typeof(EventSource)!=="undefined") 
      { 
          var source=new EventSource("bookcounterevent.php"); 

          source.onmessage=function(event) 
          { 
              if(parseInt(event.data) > 0){ 
                document.getElementById("bookcounter").innerHTML=event.data; 
              } 
              else{ 
                document.getElementById("nonotification").innerHTML= 'nothing to display'; 
              } 
          }
      } 
      else 
      { 
        document.getElementById("bookcounter").innerHTML="Please update your browser!!"; 
      } 
  </script>

<div id="notification"></div>
<div id="nonotification"></div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.