calling a variable in javascript from php or vice versa

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • myname
    New Member
    • Mar 2006
    • 1

    calling a variable in javascript from php or vice versa

    i need help with referencing variables from php to a javascript or vice versa.

    my pages are written in php primarily. i needed a user response - therefore i used javascript and used confirm() to get the appropriate response.
    the user response is needed inside the php for further calculations.

    does anybody have any idea?

    any kind of help would be much appreciated.
    thanks,
    :confused:
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    php runs server side and javascript runs client side

    There is no point using a confirm as this just makes it harder, use a form and submit the form to a php script. Depending on the submit method (GET or POST) the data in the form will appear in the php superglobal $_GET or $_POST which are arrays the indexes of which will be the names you give your form element.

    Comment

    • sankviju
      New Member
      • Jun 2006
      • 3

      #3
      Hi

      You can directly embed php in java script

      like
      <script language="javas cript">
      function x()
      {
      <?php

      ur php code


      ?>


      }

      then execute where ever u want

      onclick or onsubmit or any where if u need more help just inform me

      </script>

      Comment

      • kithkanan
        New Member
        • Jul 2006
        • 2

        #4
        Originally posted by sankviju
        Hi

        You can directly embed php in java script

        like
        <script language="javas cript">
        function x()
        {
        <?php

        ur php code


        ?>


        }

        then execute where ever u want

        onclick or onsubmit or any where if u need more help just inform me

        </script>

        hey i have:

        <SCRIPT LANGUAGE="JavaS cript">
        function test(){
        <?php

        echo "hi";
        ?>
        }
        </SCRIPT>

        <a href="javascrip t:test();">test </a>

        but it doesn't work. :(

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by kithkanan
          hey i have:

          <SCRIPT LANGUAGE="JavaS cript">
          function test(){
          <?php

          echo "hi";
          ?>
          }
          </SCRIPT>

          <a href="javascrip t:test();">test </a>

          but it doesn't work. :(
          You are using 2 different scripting languages, PHP and Javascript.

          PHP runs server side and Javascript (normally) runs client side so by the time the browser has loaded a page the php has already run, the Javascript runs after the browser has loaded the page.

          The effect of your code above is that the browser sees a page with the following in it

          [html]
          <SCRIPT LANGUAGE="JavaS cript">
          function test(){
          hi }
          </SCRIPT>

          <a href="javascrip t:test();">test </a>
          [/html]

          "hi" is not valid Javascript so it doesn't work. You could have

          [php]
          <SCRIPT LANGUAGE="JavaS cript">
          function test(){
          document.write( <?php echo '"hi"'; ?>);
          }
          </SCRIPT>

          <a href="javascrip t:test();">test </a>
          [/php]

          or

          [php]
          <SCRIPT LANGUAGE="JavaS cript">
          function test(){
          var string = <?php echo '"hi"'; ?>;
          document.write( string);
          }
          </SCRIPT>

          <a href="javascrip t:test();">test </a>
          [/php]

          Comment

          • iam_clint
            Recognized Expert Top Contributor
            • Jul 2006
            • 1207

            #6
            <? $test = "test"; ?>
            Code:
             <SCRIPT LANGUAGE="JavaScript">
            function test(){
                var string = "<?=$test?>";
                alert(string);
            }
            </SCRIPT>
            
            <a href="javascript:test();">test</a>
            this would be kind of passing a variable..

            Comment

            • pauloeduardomaia
              New Member
              • Nov 2006
              • 1

              #7
              Hey,

              Produce more incredible result if you write your script like this:

              print
              "<script language=javasc ript>
              function test()
              {
              var string='$test';
              alert(string);
              }
              </script>
              ";



              Originally posted by iam_clint
              <? $test = "test"; ?>
              Code:
               <SCRIPT LANGUAGE="JavaScript">
              function test(){
                  var string = "<?=$test?>";
                  alert(string);
              }
              </SCRIPT>
              
              <a href="javascript:test();">test</a>
              this would be kind of passing a variable..

              Comment

              Working...