js接收post/get值
时间:2022-11-24 15:09 作者:六度科技 分类: JS
注意必须文件为.php
//先用php接收post值
<?php
$user_p=trim($_POST['user']);
$pswd_p=trim($_POST['pswd']);
?>
//再用echo方法把值反馈给js 注意要用''包裹转成字符串
//否则出错
<script>
var pswd='<?php echo $pswd_p ?>';
var user='<?php echo $user_p ?>';
console.log(user);
console.log(pswd);
</script>
附带接收get
方法1
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}
var user=getQueryString('user');
var pswd=getQueryString('pswd');
方法2
function getPar(par){
//获取当前URL
var local_url = document.location.target="blank" href;
//获取要取得的get参数位置
var get = local_url.indexOf(par +"=");
if(get == -1){
return false;
}
//截取字符串
var get_par = local_url.slice(par.length + get + 1);
//判断截取后的字符串是否还有其他get参数
var nextPar = get_par.indexOf("&");
if(nextPar != -1){
get_par = get_par.slice(0, nextPar);
}
return get_par;
}
方法3
(返回 $_GET 对象, 仿PHP模式)
var $_GET = (function(){
var url = window.document.location.target="blank" href.toString();
var u = url.split("?");
if(typeof(u[1]) == "string"){
u = u[1].split("&");
var get = {};
for(var i in u){
var j = u[i].split("=");
get[j[0]] = j[1];
}
return get;
} else {
return {};
}
})();