how to pass php value to javascript function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sweetline priya
    New Member
    • Jan 2009
    • 16

    how to pass php value to javascript function?

    hii friends..

    i am in need of a coding.. can u help me??

    how to pass a php variable as an argument to javascript function?? is there any possible ways???
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Echo your variable into the javascript..

    Code:
    doFunction("<?php echo $var; ?>");
    
    function doFunction(arg)
    {
        // ...
    }

    Comment

    • sweetline priya
      New Member
      • Jan 2009
      • 16

      #3
      Originally posted by Markus
      Echo your variable into the javascript..

      Code:
      doFunction("<?php echo $var; ?>");
      
      function doFunction(arg)
      {
          // ...
      }
      can u explain how to call this function ?? within php coding?? or in onSubmit, onclick events??

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Code:
        <a href="#" onclick="doFunction('<?php echo $var; ?>');">Click</a>

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by sweetline priya
          can u explain how to call this function ?? within php coding?? or in onSubmit, onclick events??
          The main thing to realize here is that PHP is a server-side language, while JavaScript is a client-side language.

          They can not directly interact with each other. That is; PHP can not directly call a JavaScript function or access JavaScript values, and JavaScript can not directly call a PHP function or access PHP values.

          However, because PHP is server-side, you can have PHP print JavaScript code, which will then be executed by the client when it reaches it.
          Which also means that you can have PHP print values directly into JavaScript code, like Markus demonstrated.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by Atli
            The main thing to realize here is that PHP is a server-side language, while JavaScript is a client-side language.

            They can not directly interact with each other. That is; PHP can not directly call a JavaScript function or access JavaScript values, and JavaScript can not directly call a PHP function or access PHP values.

            However, because PHP is server-side, you can have PHP print JavaScript code, which will then be executed by the client when it reaches it.
            Which also means that you can have PHP print values directly into JavaScript code, like Markus demonstrated.
            I was hoping you'd explain.. I don't have the patience today.

            Comment

            • sweetline priya
              New Member
              • Jan 2009
              • 16

              #7
              thank you 4 ur replies... :)

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Glad we could be of service!

                Comment

                Working...