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

How to call php method parameters in js

coldplay.xixi
coldplay.xixiOriginal
2021-02-23 17:18:542537browse

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>

php:

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

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){
})
…………

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"]);

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!

Statement:
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