Use Javascript variable in PHP

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

    Use Javascript variable in PHP

    I am a beginner in programming. I just want to know how can i use a variable used in javascript in my php coding.


    Code:
    <?php
    echo '<script language="JavaScript">
    var ace= prompt("Enter your name please", "Name");
    </script>';
    ?>
    
    <html>
    simple html coding..
    <?php function_name(ace in script);?>
    </html>
    
    <?php
    function_name($ace)
    {
    echo'your name is'.$ace.'!';
    }
    ?>
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    PHP runs on the web server where as JavaScript runs in the browser. Since they run in completely different environments on completely different computers, these two technologies cannot share variables...

    However, both can access HTML elements...like HiddenFields or Cookies.

    So, in your PHP code, set the value of a HiddenField element so that when your JavaScript runs it can retrieve the value from the HiddenField.... You can also set the value of the HiddenField using JavaScript so that you can retrieve it in your PHP code.

    -Frinny

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Just use JavaScript. Simples.

      Comment

      Working...