JS call PHP and PHP call JS Method Example

Source: Internet
Author: User
Tags echo date
js call php and PHP call JS method, small series to make the following summary, hope to help everyone;

First: JS method to call the PHP file and get the value in PHP , the following examples:
such as in the page test_1 with the following sentence call:

<!--example, this JS file does not exist, the JS file is changed to your own--><script type= "Text/javascript" src= "Http://www.php.cn/a.js" ></ Script><script type= "Text/javascript" >    alert (jstext);</script>

test. php File:

<?php$php_test= ' I like topic.alibabacloud.com! '; echo "var test= ' $php _test ';"; echo "var jstext=". "' $php _test ';? >

When the test. php file is executed, the test_1. php file is called and the output of the b.php file is executed as a JS statement.

So here will be a popup box, the contents of the JS variable jstext value, that is, the PHP file assigned to Jstext value.

Summary: In the HTML with JS call file in the way the PHP file, the output of the PHP file will be called the page as JS code to use.
PHP calls the value in JS
There is a piece of code in the test.php page:

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

PHP calls the method (function) in JS
Similar to the second case, using the Echo script to implement the JS call

<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>";?>

JS Call PHP variable (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 variables? ><script type= "Text/javascript" >    //js call php variable    var ds = "<?php echo $url?>";//Assignment    Alert (DS); Output Effects </script>

JS Call php function

<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>
Related Article

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.