Converting JavaScript variable to php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shalini Bhalla
    New Member
    • Dec 2007
    • 190

    Converting JavaScript variable to php

    is there any method by which i can convert javascript variables to php variables
  • rizwan6feb
    New Member
    • Jul 2007
    • 108

    #2
    Most of the begginers try to do this?
    It doesn't make any sense assigning javascript variables to PHP variables. You can pass the values of javascript variables to a PHP script, where you can get those values into PHP variables. There are different ways of passing values from client (browser) to PHP script

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Could you explain in what situation you would need to do this?

      Thanks.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Originally posted by Shalini Bhalla
        is there any method by which i can convert javascript variables to php variables
        Javascript can not interact with PHP directly.
        This is due to the fact that Javascript is run on the client's browser (client-side) while PHP is executed on the server (server-side).
        So, before your Javascript code is even read by the browser, all of the PHP code will already have been executed by the server.

        There are two ways to have Javascript communicate with PHP.
        Either you have to refresh/redirect the page, sending along the data via the POST or GET protocols.
        Or, you can use AJAX.

        I would still be very careful when using data sent by Javascript. As it is generated by the client's browser, it can easily be manipulated before it is sent.
        Make sure you validate it before you use it.

        Comment

        • sandeepsandeep
          New Member
          • Dec 2006
          • 50

          #5
          Not,
          u can use php variable in javscript but cant use javascript variable in PHP because php can not parrsing the javascript value....

          Regards,
          Sandeep agarwal

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by sandeepsandeep
            ..u can use php variable in javscript ...
            How ?

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Originally posted by r035198x
              How ?
              I'm guessing hes talking about just echoing PHP values into the Javascript code?

              For the record, using that method, the Javascript code is in fact not using a PHP variable. The PHP code is used to create a Javascript variable, which Javascript can then use.

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                "Converting " a variable from JavaScript to PHP or vice versa is kind of like trying to "convert" a 20-Euro note into a butternut squash. You have to go through the grocer, or else you're bound to get some funny looks.

                And for 20 Euros, it better be one heckuva butternut squash. I'd say this is about the point where the analogy breaks down.

                Anyway, PHP is executed on the server side, whereas JavaScript works on the client side, and the two *never talk to each other*.

                In fact, PHP technically doesn't even know that the User's computer even exists; the HTTP server handles all those details.

                PHP can output JavaScript code that (once the User's browser executes that code) will create a JavaScript variable with a particular value.

                You can also pass parameters in an HTTP request that PHP converts into elements in the $_GET superglobal.

                But the "conversion " process has to be done in a roundabout way because, as noted before, PHP and JavaScript don't talk to each other, and in fact, they are not even aware of each other's existence.

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  Originally posted by pbmods
                  "Converting " a variable from JavaScript to PHP or vice versa is kind of like trying to "convert" a 20-Euro note into a butternut squash. You have to go through the grocer, or else you're bound to get some funny looks.
                  Brilliant analogy :)
                  Although I am not really sure what a butternut squash is.

                  Comment

                  • pbmods
                    Recognized Expert Expert
                    • Apr 2007
                    • 5821

                    #10
                    Originally posted by Atli
                    Brilliant analogy :)
                    Although I am not really sure what a butternut squash is.


                    Squtterbut Nash!

                    Comment

                    • Atli
                      Recognized Expert Expert
                      • Nov 2006
                      • 5062

                      #11
                      wow.. that thing looks dangerous :P

                      Comment

                      • coolsti
                        Contributor
                        • Mar 2008
                        • 310

                        #12
                        Actually, it is quite tasty! The meat is like pumpkin, just a bit more compact. Great for stews and it gives a bit of a flare to stir fry.

                        Comment

                        • zorgi
                          Recognized Expert Contributor
                          • Mar 2008
                          • 431

                          #13
                          Originally posted by coolsti
                          Actually, it is quite tasty! The meat is like pumpkin, just a bit more compact. Great for stews and it gives a bit of a flare to stir fry.
                          I don't like pumpkin

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by coolsti
                            Actually, it is quite tasty! ....
                            Agreed .

                            Comment

                            • Markus
                              Recognized Expert Expert
                              • Jun 2007
                              • 6092

                              #15
                              From javascript variables in PHP, to Butternut Squash.

                              Just another day on Bytes.com

                              Comment

                              Working...