Thursday, January 28, 2016

Passing a Variable Between HTML, PHP, and Javascript.

Passing a Variable Between HTML, PHP, and Javascript.

Task: Pass a variable from an HTML form to a PHP handler.  Then Pass that variable into Javascript.

Pseudo Code.

    1. Enter data into an HTML text box.
    2. When the submit button is clicked POST the data into the PHP handler
    3. Place posted data into a PHP variable
    4. Pass the PHP variable into a Javascript variable.

HTML Code.

 <body>
<form action="passphphandler.php" method="post">
  Enter Text:<br>
  <input type="text" name="stringtext" value="">
  <br>
  <br>
  <input type="submit" value="Submit">
</form>
  </body>


PHP Code.

<body>
 
<?php
$php_var =$_POST["stringtext"];
?>


    <script>
      var js_var = "<?php echo $php_var; ?>";
       alert(js_var);

  </body>