[JavaScript] example of how Javascript is called by JS when PHP and PHP are called

Source: Internet
Author: User
Tags echo date

1. Call the PHP file in JS mode and obtain the value in PHP.
Here is a simple example:
For example, call the following statement in test_json1:

<script type="text/javascript" src="http://callmewhy.sinaapp.com/index.php/test/testjson2"></script><script type="text/javascript" >    alert(jstext);</script>

There is such a PHP code in test_json2.php:

<?php$php_test='I come from PHP!';echo "var test='$php_test';";echo "var jstext="."'$php_test';"; ?> 

When the test_json1.php file is executed, the test_json2.php file is called and the output of the B. php file is executed as a JS statement,

Therefore, a prompt box is displayed, containing the value of the JS variable jstext, that is, the value assigned to jstext in the PHP file.


Summary: when PHP files are called by JS in HTML, the output of PHP files will be called by pages as JS Code.

2. php calls the value in JS

The test_json3.php page contains the following code:

<script type="text/javascript" > var data="call_me_why"; </script><? echo "<script type=text/javascript>document.write(data)</script>";?>

3. php calls methods (functions) in JS)
Similar to the second scenario, the echo script is used to implement JS calls.

<script type="text/javascript"> function test() {   var t1=3;   t1 = t1+2;   alert(t1);   //return t1; } </script><?php echo "<script type='text/javascript'>test();</script>"; ?> 

4. js calls PHP Variables
(1)

<?php$userId=100;?> <script>var userId;userId=document.getElementById("userId").value;alert(userId);</script><input type="text" name="userId" id="userId" value="<?php echo $userId; ?>">

(2)

<? PHP $ url = 'call _ me_why '; // define the variable?> <SCRIPT type = "text/JavaScript"> // JS calls the PHP variable var ds = "<? PHP echo $ URL?> "; // Assign a value to alert (DS); // output result </SCRIPT>

5. js calls PHP Functions

<script language="JavaScript">var Y=<?php echo date('Y')?>,M=<?php echo date('n')?>,D=<?php echo date('j')?>;alert(Y);alert(M);alert(D);</script>

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.