How to get value of the get or post variable on page load using JavaScript?
This question is related to
javascript
With little php is very easy.
HTML part:
<input type="text" name="some_name">
JavaScript
<script type="text/javascript">
some_variable = "<?php echo $_POST['some_name']?>";
</script>
/**_x000D_
* getGET: [Funcion que captura las variables pasados por GET]_x000D_
* @Implementacion [pagina.html?id=10&pos=3]_x000D_
* @param {[const ]} loc [capturamos la url]_x000D_
* @return {[array]} get [Devuelve un array de clave=>valor]_x000D_
*/_x000D_
const getGET = () => {_x000D_
const loc = document.location.href;_x000D_
_x000D_
// si existe el interrogante_x000D_
if(loc.indexOf('?')>0){_x000D_
// cogemos la parte de la url que hay despues del interrogante_x000D_
const getString = loc.split('?')[1];_x000D_
// obtenemos un array con cada clave=valor_x000D_
const GET = getString.split('&');_x000D_
const get = {};_x000D_
_x000D_
// recorremos todo el array de valores_x000D_
for(let i = 0, l = GET.length; i < l; i++){_x000D_
const tmp = GET[i].split('=');_x000D_
get[tmp[0]] = unescape(decodeURI(tmp[1]));_x000D_
}//::END for_x000D_
return get;_x000D_
}//::END if _x000D_
}//::END getGET_x000D_
_x000D_
/**_x000D_
* [DOMContentLoaded]_x000D_
* @param {[const]} valores [Cogemos los valores pasados por get]_x000D_
* @return {[document.write]} _x000D_
*/_x000D_
document.addEventListener('DOMContentLoaded', () => {_x000D_
const valores=getGET();_x000D_
_x000D_
if(valores){_x000D_
// hacemos un bucle para pasar por cada indice del array de valores_x000D_
for(const index in valores){_x000D_
document.write(`<br>clave: ${index} - valor: ${valores[index]}`);_x000D_
}//::END for_x000D_
}else{_x000D_
// no se ha recibido ningun parametro por GET_x000D_
document.write("<br>No se ha recibido ningún parámetro");_x000D_
}//::END if_x000D_
});//::END DOMContentLoaded
_x000D_
// 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
Here is my answer for this given a string returnURL which is like http://host.com/?param1=abc¶m2=cde. It's fairly basic as I'm beginning at JavaScript (this is actually part of my first program ever in JS), and making it simpler to understand rather than tricky.
Notes
this is only for GET, and not 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;
}
}
}
The simplest technique:
If your form action attribute is omitted, you can send a form to the same HTML file without actually using a GET HTTP access, just by using onClick on the button used for submitting the form. Then the form fields are in the elements array document.FormName.elements . Each element in that array has a value attribute containing the string the user provided (For INPUT elements). It also has id and name attributes, containing the id and/or name provided in the form child elements.
When i had the issue i saved the value into a hidden input:
in html body:
<body>
<?php
if (isset($_POST['Id'])){
$fid= $_POST['Id'];
}
?>
... then put the hidden input on the page and write the value $fid with php echo
<input type=hidden id ="fid" name=fid value="<?php echo $fid ?>">
then in $(document).ready( function () {
var postId=document.getElementById("fid").value;
so i got my hidden url parameter in php an js.
You can only get the URI arguments with JavaScript.
// 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("+", " "));
}
}
This is my first Answer in stackoverflow and my english is not good. so I can't talk good about this problem:)
I think you might need the following code to get the value of your or tags.
this is what you might need:
HTML
<input id="input_id" type="checkbox/text/radio" value="mehrad" />
<div id="writeSomething"></div>
JavaScript
function checkvalue(input , Write) {
var inputValue = document.getElementById(input).value;
if(inputValue !="" && inputValue !=null) {
document.getElementById(Write).innerHTML = inputValue;
} else {
document.getElementById(Write).innerHTML = "Value is empty";
}
}
also, you can use other codes or other if in this function like this:
function checkvalue(input , Write) {
var inputValue = document.getElementById(input).value;
if(inputValue !="" && inputValue !=null) {
document.getElementById(Write).innerHTML = inputValue;
document.getElementById(Write).style.color = "#000";
} else {
document.getElementById(Write).innerHTML = "Value is empty";
}
}
and you can use this function in your page by events like this:
<div onclick="checkvalue('input_id','writeSomthing')"></div>
I hope my code will be useful for you
Write by <Mehrad Karampour>
Source: Stackoverflow.com