Introduction to mutual passing of values between js and php

Source: Internet
Author: User

JS is the front-end language, PHP is the background language, and the front and back-end are often unclear at the beginning (I did it at the beginning, and now I am also committing a crime ), my original idea was to regard the frontend and backend as two islands. They cannot be crossed. HTML is like a bridge. When you want to pass the variables on one island to another, only with the help of this bridge.
Let me make a small summary:
1: how to upload the values in HTML to JS? The following is a file named 1.php.
Copy codeThe Code is as follows: <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>

JS, if you want to get the name value entered by the user in the text box, writeCopy codeThe Code is as follows: <script language = 'javascript '>
Function get ()
{
Var n = document. getElementById ('username'). value;
Alert (n );
} </Script>

In this case, a warning box will pop up when JS get () is called, and the content in the box is the value of name.
2: If the name value obtained in JS is to be returned to the rename text box, write
Copy codeThe Code is as follows: <script language = 'javascript '>
Function get ()
{
Var n = document. getElementById ('username'). value;
Document. getElementById ("username1"). value = n;
} </Script>

In this way, calling get () below will automatically display the value entered by your name above.
3: Get the page value in PHP
I think everyone will.Copy codeThe Code is as follows: <? Php
$ Name = $ _ REQUEST ["username"];
Echo $ name;
?>

4: PHP value returned to the page
Insert the PHP language into HTML. You can call the value of the variable in PHP or use Smarty (recommended ).
With the above, both the values on the HTML page and the values of the JS variables can be easily imported into PHP. Of course, the values of PHP can also be uploaded to the desired place.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.