Home > Article > Backend Development > How to call php method parameters in js
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()].
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['keyword'])) ? $_GET['keyword'] : ”;
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
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!