


This article mainly introduces a summary of the methods of JS calling PHP and PHP calling JS. It has certain reference value. Now I share it with you. Friends in need can refer to it.
1 JS method of calling PHP file and get the value in php
Give a simple example to illustrate:
For example, in page a.html, use the following sentence to call:
<script type="text/javascript" src="b.php?action=test"></script> <script type="text/javascript" > alert(jstext); </script>
In b. There is such a piece of PHP code in php:
<? $action=$_GET['action']; //echo "var jstext='$action'"; //输出一句JS语句,生成一个JS变量,并赋颠值为PHP变量 $action的值 //echo "var jstext='aa'"; echo "var jstext="."'$action'"; ?>
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 pop-up will appear here A prompt box whose content is the value of the JS variable jstext, which is the value assigned to jstext in the PHP file.
Summary:
Use JS to call the file in HTML to call the PHP file , then the output of the PHP file 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; ?>
3 php calls methods (functions) 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 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; ?>">
(2)
<?php$url = '变化的网址'; //定义变量?><script type="text/javascript">//js调用php变量var ds ="<?php echo $url?>" ; //赋值 alert(ds); //输出效果 </script> 5 ------------------------------- <script language="JavaScript"> <!-- var Y=<?php echo date('Y')?>,M=<?php echo date('n')?>,D=<?php echo date('j')?>; --> </script>
6 The js and php written by myself call each other
1.php content:
<?php //echo "<script LANGUAGE='javascript'>alert('$php变量');</script>"; //最简单的php调用js //echo "<a href=#><img style="max-width:90%"Summary of methods for calling php from js and calling js from php" ></a>"; //echo "<a href='3.php'>aaaa</a>"; //php中超链接 //echo "<script type='text/javascript' language='javascript'>phpmake('PHP建站学习笔记网');</script>"; //有时候需要在PHP执行过程中,需要调用JavaScript自定义函数(验证时出错) echo "function ok(msg){alert(msg);}"; ?>
<HTML> <HEAD> <TITLE> php调用js文件的好办法</TITLE> </HEAD> <BODY> <!--js调用php中定义的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调用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'm OK!"; } else { echo "I'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>
<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('<?php echo 'Y';?>'); } } </script> </head> <body> <?php $email = "aa"; ?> <input type=button value=click onclick='test() '> </body> </html> <!--php中含有js代码--> <?php echo "<script language=javascript> function test(){ alert( 'hello '); } </script> "; ?> <input type=button value=click onclick='test() '>
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
PHP generates QR code for WeChat applet with parameters
For the five principles of PHP object-oriented design ( SOLID) Summary
The above is the detailed content of Summary of methods for calling php from js and calling js from php. For more information, please follow other related articles on the PHP Chinese website!

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor
