JS call PHP and PHP to invoke JS Method Example
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 to the 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 popup box will appear, with the contents of the value of the JS variable jstext, and
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 '; Defining variables
?>
<script type= "Text/javascript" >
JS Call PHP variable
var ds = "<?php echo $url?>"; Assignment alert (DS); Output Effects </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 to JS
echo "<a href=#></a> ";
echo "<a href= ' 3.php ' >aaaa</a>"; Hyperlinks in PHP
echo "<script type= ' text/javascript ' language= ' JavaScript ' >phpmake (' PHP Build Station learning Note net ');</script>"; There are times when PHP is required to
Line procedure, you need to call the JavaScript custom function (Error validating)
echo "function ok (msg) {alert (msg);}";
?>
<HTML>
<HEAD>
<TITLE> php good way to invoke JS file </TITLE>
</HEAD>
<BODY>
<!--JS calls the js--> defined in PHP
<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-->
<?php
if ($_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>
<body>
<a href= "#" href= "#" onclick= "Javascript:func ();" >please click</a>
</body>
<!--JS Call php-->
<script>
function IsMail (poststring)
{
re=/\w*/
if (Re.test (poststring))
{
return true;
}
Else
{
return false;
}
}
function Test () {
if (IsMail (<?php echo $email?>))
{document.write ("<?php echo" N ";? > ");}
Else
{document.write (' <?php echo ' Y ';? > ');}
}
</script>
<body>
<?php
$email = "AA";
?>
<input Type=button value=click onclick= ' Test () ' >
</body>
<!--php contains JS code--
<?php
echo "
<script language=javascript>
function Test () {
Alert (' Hello ');
}
</script> ";
?>
<input Type=button value=click onclick= ' Test () ' >
JS call PHP and PHP to invoke JS Method Example