Introduction to js and php cross pass values

  • 2020-06-19 10:01:15
  • OfStack

JS is the language of the front desk, PHP is the language of the background, the beginner often occurs when Taiwan before and after confused situation now (I was originally such, sometimes making), my original idea is to put as Taiwan before and after the two island, they are unable to cross, HTML is like a bridge, when you want to put the variables of the 1 island to the other one on the island, only with the aid of the bridge.
So let me just conclude with a little bit:
1: How is the value in HTML transmitted to JS

<html>
             <body>
                <form action="1.php" method="post">
                     name:<input type="text" name="username" id="username">
                    rename:<input type="text" name="username1" id="username1">
                    <input type="button" value="submit" on CliCk="get()">
               </form>
              </body>
         </html>

If you want to take the name value entered by the user in the text box, write this

<script language='JavaScript'>
function get()
{
var n=document.getElementById('username').value;
alert (n);
}</script>

This will cause an alert box to pop up whenever JS get() is called, and the contents will be the value of name.
2: If the name value retrieved from JS is returned to the rename text box, write this

<script language='JavaScript'> 
function get() 
{ 
   var n=document.getElementById('username').value; 
   document.getElementById("username1").value=n; 
}</script>

Calling get() below will automatically display the value you entered in name above.
3: Take the value of the page in PHP
I think you're all familiar with that

<?php
$name=$_REQUEST["username"];
echo $name;
?>

4: The value of PHP is returned to the page
Insert the PHP language into HTML to call the value of the variable in PHP or Smarty (recommended).
With this in mind, you can easily pass in PHP values from both the HTML page and JS variables, and of course, PHP values can also be passed in wherever you want.

Related articles: