Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting value GET OR POST variable using JavaScript?

Tags:

javascript

How to get value of the get or post variable on page load using JavaScript?

like image 660
Mohit Jain Avatar asked Dec 25 '09 12:12

Mohit Jain


People also ask

Can JavaScript receive POST data?

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.

How do you call a variable value in JavaScript?

Example: var MyVariable = "Value of variable"; function readValue(name) { .... } alert(readValue("MyVariable"));

How do you find the value of a variable?

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.

Can JavaScript send POST request?

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.


3 Answers

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.

like image 153
Igor Zinov'yev Avatar answered Oct 25 '22 00:10

Igor Zinov'yev


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("+", " "));
    }
}
like image 38
Gumbo Avatar answered Oct 25 '22 02:10

Gumbo


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>

like image 1
MEH Avatar answered Oct 25 '22 01:10

MEH