Home > Backend Development > PHP Problem > How to call php js method

How to call php js method

藏色散人
Release: 2023-03-08 06:06:02
Original
3571 people have browsed it

php js calling method: first open the corresponding code file; then pass the PHP code "test();" to call the js method.

How to call php js method

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer.

Examples of methods for js to call php and php to call js

1. JS method calls PHP files and obtains the value in php

Give a simple example to illustrate:

For example, use the following sentence in page a.html:

<script type="text/javascript" src="b.php?action=test"></script>
<script type="text/javascript" >
alert(jstext);
</script>
Copy after login

There is such a piece of PHP code in b.php:

<? 
$action=$_GET[&#39;action&#39;]; 
//echo "var jstext=&#39;$action&#39;"; 
//输出一句JS语句,生成一个JS变量,并赋颠值为PHP变量 $action的值 
//echo "var jstext=&#39;aa&#39;"; echo "var jstext="."&#39;$action&#39;"; 
?>
Copy after login

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

Recommended: "PHP Video Tutorial"

Summary:

Use JS to call files in HTML to adjust PHP files, then the PHP file The output will be used as JS code by the calling page.

2. PHP calls the value in js

There is such 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; ?>
Copy after login

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=&#39;text/javascript&#39;>test();
</script>"; ?>
Copy after login

4. JS calls PHP variables

(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; ?>">
Copy after login

(2)

<?php
$url = &#39;变化的网址&#39;;    //定义变量
?>
<script type="text/javascript">
//js调用php变量
var ds ="<?php echo $url?>" ; //赋值 alert(ds); //输出效果 </script>
5  -------------------------------
<script language="JavaScript"> <!--
var Y=<?php echo date(&#39;Y&#39;)?>,M=<?php echo date(&#39;n&#39;)?>,D=<?php echo date(&#39;j&#39;)?>;
-->
</script>
Copy after login

6 The js and php you wrote call each other

1.php content:

<?php
 
//echo "<script LANGUAGE=&#39;javascript&#39;>alert(&#39;$php变量&#39;);</script>";   //最简单的php调用js
//echo "<a href=#><img width=50 src=&#39;$fruit_pic_array[$i]&#39; onMouseOver=’javascript:a();‘></a>";
//echo "<a href=&#39;3.php&#39;>aaaa</a>";   //php中超链接
 
//echo "<script type=&#39;text/javascript&#39; language=&#39;javascript&#39;>phpmake(&#39;PHP建站学习笔记网&#39;);</script>";   //有时候需要在PHP执行过程中,需要调用JavaScript自定义函数(验证时出错)
echo "function ok(msg){alert(msg);}";
?>
<HTML>
<HEAD>
<TITLE> php调用js文件的好办法</TITLE>
</HEAD>
<BODY>
<!--js调用php中定义的js-->
<scrīpt language=&#39;&#39;javascrīpt&#39;&#39; type=&#39;&#39;text/javascrīpt&#39;&#39; src=&#39;&#39;1.php&#39;&#39;></scrīpt>  
<scrīpt>
ok("aaaaaa!");
</scrīpt>
</script>
</BODY>
</HTML>
Copy after login

2.php content:

<!--js调用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调用php-->
 
<?php
 
if($_GET["action"]=="ok")
 
{
 
echo "I&#39;m OK!";
 
}
 
else
 
{
 
echo "I&#39;m not OK!";
 
}
 
?>
 
<SCRIPT Language = "JavaScript">
 
function func()
 
{
 
if(confirm("Are you OK with this?"))
 
{
 
this.location = "ok.php?action=ok";
 
}
 
else
 
{
 
this.location = "ok.php?action=cancel";
 
}
 
}
 
</SCRIPT>
 
<html>
 
<head>
 
</head>
 
<body>
 
<a href="#" href="#" onClick="javascript:func();">Please Click</a>
 
</body>
 
</html>
Copy after login

<html>
<head>
    <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(&#39;<?php echo &#39;Y&#39;;?>&#39;);
            }
        }
    </script>
</head>
<body>
<?php
$email = "aa";
?>
 
<input type=button value=click onclick=&#39;test() &#39;>
</body>
</html>
 
 
<!--php中含有js代码-->
 
<?php
echo "<script   language=javascript>
  function test(){
    alert( &#39;hello &#39;);   
  }
</script> ";
?>
<input type=button value=click onclick=&#39;test() &#39;>
Copy after login

The above is the detailed content of How to call php js method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template