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

How to call php method parameters in js

coldplay.xixi
Release: 2023-03-08 08:16:01
Original
2621 people have browsed it

The method for js to call php method parameters: 1. Direct URL encoding, the code is [string = encodeURIComponent()]; 2. Use escape encoding, the code is [$.getJSON("admin.php?action= ” escape()].

How to call php method parameters in js

The operating environment of this tutorial: windows7 system, javascript1.8.5&&PHP5.6 version, DELL G3 computer, this method is suitable for all brands Computer.

Methods for js to call php method parameters:

The first method, Direct URL encoding is more convenient

JS :

<script type=”text/javascript”>
string = encodeURIComponent(string);
location.href = index.php?keyword=’+string;
</script>
Copy after login

php:

$keyword = (isset($_GET[&#39;keyword&#39;])) ? $_GET[&#39;keyword&#39;] : ”;
Copy after login

The second type, using escape encoding is more complicated but seems to be more versatile. It may be required when passing through ajax #JS:

……..
$.getJSON(“admin.php?action=”+escape(action),function(json){
})
…………
Copy after login

PHP:

function unescape($str) { //定义unescape函数
$str = urldecode($str);
preg_match_all(“/(?:%u.{4}|&#x.;|&#d+;|.+)/U”,$str,$r);
$ar = $r[0];
foreach($ar as $k=>$v) {
if(substr($v,0,2) == “%u”)
$ar[$k] = iconv(“UCS-2BE”,”utf-8″,pack(“H4″,substr($v,-4)));
elseif(substr($v,0,3) == “&#x”)
$ar[$k] = iconv(“UCS-2BE”,”utf-8″,pack(“H4″,substr($v,3,-1)));
elseif(substr($v,0,2) == “&#”) {
$ar[$k] = iconv(“UCS-2BE”,”utf-8″,pack(“n”,substr($v,2,-1)));
}
}
return join(“”,$ar);
}
$action=unescape($_GET["action"]);
Copy after login

Related free learning recommendations:
php programming

(video)

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

Related labels:
js
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
Latest Issues
Where is js written?
From 1970-01-01 08:00:00
0
0
0
js file code not found
From 1970-01-01 08:00:00
0
0
0
js addClass not working
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template