JS call PHP and PHP call JS method Summary

Source: Internet
Author: User
Tags echo date
This article mainly introduces the JS call PHP and PHP call JS method Summary, has a certain reference value, now share to everyone, the need for friends can refer to

1 JS way to call PHP file and get the value in PHP

Give a simple example to illustrate:

As in the page a.html the following sentence is called:

<script type= "Text/javascript" src= "b.php?action=test" ></script><script type= "Text/javascript" > Alert (Jstext);</script>

There is a PHP code in b.php:

<? $action =$_get[' action ']; echo "var jstext= ' $action '"; Output A JS statement, generate a JS variable, and assign a value of PHP variable $action value//echo "var jstext= ' AA '"; echo "var jstext=". "' $action ' ";?>

When the a.html file is executed, the b.php file is called and the output of the b.php file is executed as a JS statement, so a prompt box pops up with the value of the JS variable jstext, which is the value assigned to Jstext in the PHP file.

Summary:

In the HTML with the JS call file to the PHP file, the output of the PHP file will be called the page as JS code to use.

2 PHP calls the value in JS

There is a piece of code in the z.php page:

<script type= "Text/javascript" > var url= "aaaa*"; </script> <? $key = "<script type=text/javascript>document.write (URL) </script>"; echo $key;?>

3 PHP calls the method (function) in JS

<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 Call PHP variable

(1)

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

(2)

<?php$url = ' changed URLs ';    Define variables? ><script type= "Text/javascript" >//js call php variable var ds = "<?php echo $url?>"; Assignment alert (DS); Output effect </script>5  -------------------------------<script language= "JavaScript" > <!--var y=<? PHP echo Date (' Y ')? >,m=<?php echo Date (' n ')? >,d=<?php Echo Date (' J ')?>;--></script>

6 Write your own JS and PHP call each other

1.php content:

<?php//echo "<script language= ' JavaScript ' >alert (' $php variable ');</script>";   The simplest PHP call Js//echo "<a href=#></a> ";//echo" <a href= ' 3.php ' >aaaa</a> ";   PHP hyperlink//echo "<script type= ' text/javascript ' language= ' JavaScript ' >phpmake (' PHP Build Station learning Note net ');</script> ";   Sometimes it is necessary to invoke the JavaScript custom function during PHP execution (error in validation) echo "function ok (msg) {alert (msg);}";? >
<HTML><HEAD><TITLE> php good way to invoke JS file </TITLE></HEAD><BODY><!-- JS calls PHP defined in js--><scrīpt language= ' javascrīpt ' type= ' text/javascrīpt ' src= ' 1.php ' ></scrīpt>  <scrīpt>ok ("aaaaaa!"); </scrīpt></script></BODY></HTML>

2.php content:

<!--JS call php--><?php$userid=100;? ><script>var userid;userid=document.getelementbyidx_x ("userid"). Value;alert (userid);</script> <input type= "text" name= "userid" id= "userid" value= "<?php echo $userId;?>" >  <!--js call php-->< PHPIF ($_get["Action"]== "OK") {echo "I ' m ok!";} Else{echo "I ' m not ok!";}? ><script Language = "JavaScript" >function func () {if (Confirm ("is OK with this?")) {this.location = "Ok.php?action=ok";} Else{this.location = "Ok.php?action=cancel";}} </script>

<!--JS Call php-->


The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.