使用JavaScript获取值GET或POSTvariables?

如何获得页面加载使用JavaScript的get或postvariables的值?

虽然可以在服务器上处理请求时将其插入到文档中,但无法使用Javascript获取POSTvariables的值。

<script type="text/javascript"> window.some_variable = '<?=$_POST['some_value']?>'; // That's for a string </script> 

GETvariables可以通过window.location.href ,有些框架甚至有方法可以parsing它们。

您只能使用JavaScript获取URI参数。

 // get query arguments var $_GET = {}, args = location.search.substr(1).split(/&/); for (var i=0; i<args.length; ++i) { var tmp = args[i].split(/=/); if (tmp[0] != "") { $_GET[decodeURIComponent(tmp[0])] = decodeURIComponent(tmp.slice(1).join("").replace("+", " ")); } } 

用小php很容易。

HTML部分:

<input type="text" name="some_name">

JavaScript的

 <script type="text/javascript"> some_variable = "<?php echo $_POST['some_name']?>"; </script> 
 // Captura datos usando metodo GET en la url colocar index.html?hola=chao const $_GET = {}; const args = location.search.substr(1).split(/&/); for (let i=0; i<args.length; ++i) { const tmp = args[i].split(/=/); if (tmp[0] != "") { $_GET[decodeURIComponent(tmp[0])] = decodeURIComponent(tmp.slice(1).join("").replace("+", " ")); console.log(`>>${$_GET['hola']}`); }//::END if }//::END for 
 /** * getGET: [Funcion que captura las variables pasados por GET] * @Implementacion [pagina.html?id=10&pos=3] * @param {[const ]} loc [capturamos la url] * @return {[array]} get [Devuelve un array de clave=>valor] */ const getGET = () => { const loc = document.location.href; // si existe el interrogante if(loc.indexOf('?')>0){ // cogemos la parte de la url que hay despues del interrogante const getString = loc.split('?')[1]; // obtenemos un array con cada clave=valor const GET = getString.split('&'); const get = {}; // recorremos todo el array de valores for(let i = 0, l = GET.length; i < l; i++){ const tmp = GET[i].split('='); get[tmp[0]] = unescape(decodeURI(tmp[1])); }//::END for return get; }//::END if }//::END getGET /** * [DOMContentLoaded] * @param {[const]} valores [Cogemos los valores pasados por get] * @return {[document.write]} */ document.addEventListener('DOMContentLoaded', () => { const valores=getGET(); if(valores){ // hacemos un bucle para pasar por cada indice del array de valores for(const index in valores){ document.write(`<br>clave: ${index} - valor: ${valores[index]}`); }//::END for }else{ // no se ha recibido ningun parametro por GET document.write("<br>No se ha recibido ningún parámetro"); }//::END if });//::END DOMContentLoaded 

当我遇到问题时,我将这个值保存到一个隐藏的input中:

在html正文中:

  <body> <?php if (isset($_POST['Id'])){ $fid= $_POST['Id']; } ?> 

然后将隐藏的input放在页面上,并用php echo写入值$ fid

  <input type=hidden id ="fid" name=fid value="<?php echo $fid ?>"> 

然后在$(document).ready(function(){

  var postId=document.getElementById("fid").value; 

所以我有我的隐藏的url参数在PHP和JS。

这是我的回答这给了一个stringreturnURL就像http://host.com/?param1=abc¶m2=cde 。 从JavaScript开始(这实际上是我在JS中的第一个程序的一部分),这是相当基本的,并且使其更易于理解而不是棘手。

笔记

  • 没有理智的价值观检查
  • 只要输出到控制台 – 你会想要将它们存储在一个数组或其他东西
  • 这只是为了GET,而不是POST

     var paramindex = returnURL.indexOf('?'); if (paramindex > 0) { var paramstring = returnURL.split('?')[1]; while (paramindex > 0) { paramindex = paramstring.indexOf('='); if (paramindex > 0) { var parkey = paramstring.substr(0,paramindex); console.log(parkey) paramstring = paramstring.substr(paramindex+1) // +1 to strip out the = } paramindex = paramstring.indexOf('&'); if (paramindex > 0) { var parvalue = paramstring.substr(0,paramindex); console.log(parvalue) paramstring = paramstring.substr(paramindex+1) // +1 to strip out the & } else { // we're at the end of the URL var parvalue = paramstring console.log(parvalue) break; } } }