Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. How to include a Javascript variable inside a php function?

How to include a Javascript variable inside a php function?

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
tutorialjavascriptphpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Glen Villar
    wrote on last edited by
    #1

    Hi! I'd like to know if there's a way I can call a javascript variable in a php function? Like for example, the code below would get the value of textbox text1 when the button is clicked. It should call the javascript function with a parameter. That javascript function calls the PHP function with a javascript variable nVal. Fuction PassNumber(nVal) { alert("<?php ShowNumber("+nVal+"); ?>"); }
    Thanks.

    G G 2 Replies Last reply
    0
    • G Glen Villar

      Hi! I'd like to know if there's a way I can call a javascript variable in a php function? Like for example, the code below would get the value of textbox text1 when the button is clicked. It should call the javascript function with a parameter. That javascript function calls the PHP function with a javascript variable nVal. Fuction PassNumber(nVal) { alert("<?php ShowNumber("+nVal+"); ?>"); }
      Thanks.

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      No. If you really want to get a value into Javascript from PHP without reloading the page, the way to do it is using AJAX.

      1 Reply Last reply
      0
      • G Glen Villar

        Hi! I'd like to know if there's a way I can call a javascript variable in a php function? Like for example, the code below would get the value of textbox text1 when the button is clicked. It should call the javascript function with a parameter. That javascript function calls the PHP function with a javascript variable nVal. Fuction PassNumber(nVal) { alert("<?php ShowNumber("+nVal+"); ?>"); }
        Thanks.

        G Offline
        G Offline
        GregStevens
        wrote on last edited by
        #3

        You have to think about it this way: PHP code works with what the server "knows". Javascript code works with what the browser / client "knows". To get them to interact, you need to explicitly "pass" information back and forth between the two. For example, if you have a variable in the javascript, you can "pass" the value to the server by passing it with a GET variable, e.g.

        <script language="javascript">
        Fuction PassNumber(nVal) {
        location.href = 'mypage.php?n='+nVal;
        }
        </script>

        Then, on the server side, you receive the GET variable to produce the effect that you want:

        <?php

        $nval = $_GET['n'];
        if ($nval)
        {
        print('<script>alert('.$nval.');</script>');
        }
        ?>

        Contrariwise, when you have the PHP variable on the server side, you can use that to feed it to the Javascript function when rendering your onclick function:

        <input type="text" name="text1" value="200" /><br />
        <input type="button" value="Go" onClick="PassNumber(this.text1.value+<?php print($nval) ?>)" />

        This requires a re-load of the page, and change of URL, each time you click the button. To do it more "subtley", without a reload, you would have to use AJAX: but the principle would be the same. Pass the information between Server and Browser using calls to the PHP page with GET.

        --Greg

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups