1, JS way to call PHP file and get the value in PHP
Give a simple example to illustrate:
As in the page Test_json1 the following sentence is called:
<script type= "Text/javascript" src= "Http://callmewhy.sinaapp.com/index.php/test/testjson2" >
</script>
<script type= "Text/javascript" >
alert (Jstext);
</script>
There is 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.
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.
2, PHP calls the value of JS
There is a piece of code in the test_json3.php page:
<script type= "Text/javascript" >
var data= "call_me_why";
</script>
<?
echo "<script type=text/javascript>document.write (data) </script>";
?>
3, PHP Call JS method (function)
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>";?>
4, 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] view plaincopy
<?php
$url = ' call_me_why '; Defining variables
?>
<script type= "Text/javascript" >
JS Call PHP variable
var ds = "<?php echo $url?>"; Assign value
Alert (DS); Output effect
</script>
5, 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>
JS call PHP and PHP to invoke JS Method Example