Web Analytics Made Easy -
StatCounter getting javascript value into php variable - CodingForum

Announcement

Collapse
No announcement yet.

getting javascript value into php variable

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • getting javascript value into php variable

    I want to get the value into php variable from javascript
    if i do like this

    Code:
    <script>
    var res = 50;
    </script>
    PHP Code:
    <?php echo "<script>document.write(res);</script>"?>
    it works.
    but,
    i want to do like this

    Code:
    var res=0;
    function add()
    {
    res =8;
    }
    </script>
    PHP Code:
    <?php echo "<script>document.write(res);</script>"?>
    it wont work. how can i do this?

  • #2
    Generally speaking, if you want to push a value from JavaScript into PHP you need to make a request. Reason being that PHP is a server-side language and JavaScript a client-side language (aside from node).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

    Comment


    • #3
      how can i do it? please suggest

      Comment


      • #4
        That depends on what you want to accomplish.
        The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
        André Behrens, NY Times Software Developer

        Comment


        • #5
          I am getting values from mqtt server and using websockets for this.

          My script is like this :
          HTML Code:
           <script type = "text/javascript">
          var connected_flag=0
          var mqtt;
          var reconnectTimeout = 2000;
          var host="localhost";
          var port=9001;
          var sub_topic="house/#";
          function onConnectionLost(){
          console.log("connection lost");
          document.getElementById("status").innerHTML = "Connection Lost";
          document.getElementById("messages").innerHTML ="Connection Lost";
          connected_flag=0;
          }
          function onFailure(message) {
          console.log("Failed");
          document.getElementById("messages").innerHTML = "Connection Failed- Retrying";
          setTimeout(MQTTconnect, reconnectTimeout);
          }
          var ot ='';
          function onMessageArrived(r_message){
          out_msg="Message received "+r_message.payloadString+"<br>";
          out_msg=out_msg+"Message received Topic "+r_message.destinationName;
          //console.log("Message received ",r_message.payloadString);
          console.log(out_msg);
          document.getElementById("messages").innerHTML =out_msg;
          var topic=r_message.destinationName;
          if(topic=="house/outside-light")
          {
          document.getElementById("outside-light").innerHTML =r_message.payloadString;
          }
          if(topic=="house/outside-temperature")
          {
          ot = r_message.payloadString; // here i am assigning the value to a variable
          
          //document.getElementById("outside-temp").innerHTML =r_message.payloadString;
          }
          }
          function onConnected(recon,url){
          console.log(" in onConnected " +reconn);
          }
          function onConnect() {
          // Once a connection has been made, make a subscription and send a message.
          document.getElementById("messages").innerHTML ="Connected to "+host +"on port "+port;
          connected_flag=1
          document.getElementById("status").innerHTML = "Connected";
          console.log("on Connect "+connected_flag);
          mqtt.subscribe(sub_topic);
          }
          
          function MQTTconnect() {
          
          console.log("connecting to "+ host +" "+ port);
          var x=Math.floor(Math.random() * 10000);
          var cname="controlform-"+x;
          mqtt = new Paho.MQTT.Client(host,port,cname);
          //document.write("connecting to "+ host);
          var options = {
          timeout: 3,
          onSuccess: onConnect,
          onFailure: onFailure,
          
          };
          
          mqtt.onConnectionLost = onConnectionLost;
          mqtt.onMessageArrived = onMessageArrived;
          //mqtt.onConnected = onConnected;
          
          mqtt.connect(options);
          return false;
          
          
          }
          function sub_topics(){
          document.getElementById("messages").innerHTML ="";
          if (connected_flag==0){
          out_msg="<b>Not Connected so can't subscribe</b>"
          console.log(out_msg);
          document.getElementById("messages").innerHTML = out_msg;
          return false;
          }
          var stopic= document.forms["subs"]["Stopic"].value;
          console.log("Subscribing to topic ="+stopic);
          mqtt.subscribe(stopic);
          return false;
          }
          function send_message(msg,topic){
          if (connected_flag==0){
          out_msg="<b>Not Connected so can't send</b>"
          console.log(out_msg);
          document.getElementById("messages").innerHTML = out_msg;
          return false;
          }
          var value=msg.value;
          console.log("value= "+value);
          console.log("topic= "+topic);
          message = new Paho.MQTT.Message(value);
          message.destinationName = "house/"+topic;
          
          mqtt.send(message);
          return false;
          }
          
          
          </script>
          And inside function onMessageArrived(r_message), i am assigning a value to a variable and if i try to display that value inside php like this
          PHP Code:
          <?php $foo="<script>document.write(ot)</script>";
          echo 
          $foo?>
          it wont work.
          i want that value to be stored in a variable.

          Comment


          • #6
            I’m not a PHP guy but for me the question that comes up is: what are you trying to do with that variable in PHP?
            What you currently have appears to me like a hacky way to do things (document.write() should be avoided at all costs); there must be a way that is more in line with the provided API.
            Stop solving problems you don’t yet have!

            Comment


            • #7
              It looks to me like you want to display this message on the page. To do that you don't need PHP, you can do that with JS.
              The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
              André Behrens, NY Times Software Developer

              Comment


              • #8
                Dormilich : yes, if it is only displaying as it comes, i dont need to store it in a variable and display. But i get some json message, in that i should display only few fields for that i have to store the complete json message in a variable and the process it/display the required fields.

                Comment


                • #9
                  Originally posted by Mythri View Post
                  But i get some json message, in that i should display only few fields for that i have to store the complete json message in a variable and the process it/display the required fields.
                  JavaScript is pretty good at processing JSON (it's called JavaScript Object Notation for a reason...), no need to use PHP for that.

                  But if you really want to use PHP, you need to send a request to PHP, let PHP process that request and send you a response, which you then have to process in JS and then display. You'd do pretty much the same that you would do in JS, only with a round trip to the server.

                  The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
                  André Behrens, NY Times Software Developer

                  Comment


                  • #10
                    Sorry, due to Corona related health issues couldn't reply in the forum.
                    @Dormilich : can you please share if any example script/tutorial (php) for this? Thanks.

                    Comment

                    Working...
                    X