How to get value of the get or post variable on page load using JavaScript?
POST data is data that is handled server side. And Javascript is on client side. So there is no way you can read a post data using JavaScript.
Example: var MyVariable = "Value of variable"; function readValue(name) { .... } alert(readValue("MyVariable"));
Use bracket notation to get an object's value by a variable key, e.g. obj[myVar] . The variable or expression in the brackets gets evaluated, so if a key with the computed name exists, you will get the corresponding value back.
To send a POST request using vanilla JavaScript, you can use an XMLHttpRequest object to interact with the server and provide the correct Content-Type request header for the POST message body data.
You can't get the value of POST variables using Javascript, although you can insert it in the document when you process the request on the server.
<script type="text/javascript">
window.some_variable = '<?=$_POST['some_value']?>'; // That's for a string
</script>
GET variables are available through the window.location.href
, and some frameworks even have methods ready to parse them.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With