How to Get Get and Post Variables with Jquery

how to get GET and POST variables with JQuery?

For GET parameters, you can grab them from document.location.search:

var $_GET = {};

document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
function decode(s) {
return decodeURIComponent(s.split("+").join(" "));
}

$_GET[decode(arguments[1])] = decode(arguments[2]);
});

document.write($_GET["test"]);

For POST parameters, you can serialize the $_POST object in JSON format into a <script> tag:

<script type="text/javascript">
var $_POST = <?php echo json_encode($_POST); ?>;

document.write($_POST["test"]);
</script>

While you're at it (doing things on server side), you might collect the GET parameters on PHP as well:

var $_GET = <?php echo json_encode($_GET); ?>;

Note: You'll need PHP version 5 or higher to use the built-in json_encode function.


Update: Here's a more generic implementation:

function getQueryParams(qs) {
qs = qs.split("+").join(" ");
var params = {},
tokens,
re = /[?&]?([^=]+)=([^&]*)/g;

while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])]
= decodeURIComponent(tokens[2]);
}

return params;
}

var $_GET = getQueryParams(document.location.search);

How to get POST variables in jQuery

$("#submit_financials").live('click', function(){
$.ajax({
url: '', // script url to send
method: 'POST', // method of sending
data: $('form').has(this).serialize(), // .serialize() make query string with form inputs name and value
dataType:'json', // expected data format returned from server, you may have something else
success: function(response) {
// response contains data returned from server
}
});
});

It would be better replace live() with .on() if you're using jQuery > 1.7 and it'd be better if possible. So you can write it

$("#container").on('click', '#submit_financials', function(){
$.ajax({
url: '', // script url to send
method: 'POST', // method of sending
data: $('form').has(this).serialize(), // .serialize() make query string with form inputs name and value
dataType:'json', // expected data format returned from server, you may have something else
success: function(response) {
// response contains data returned from server
}
});
});

Here #container point to holder of #submit_financials that belong to DOM at page load.

jQuery get post data

jQuery is for client-side Javascript. You grab POST values with server-side languages. You can provide them by mixing server-side with client-side:

<script>
(function() {
var x = "<?php echo ( isset( $_POST['name'] ) && $_POST['name'] != '') ? $_POST['name'] : '';?>";
})();
</script>

How to pass javascript/jQuery variable to PHP using POST method

Here is the code that can be function within a file:

For PHP part, you should pass value to the function and call the function.

I guess you need return data to the client from the php function return.
Then, for ajax part, you have to catch the return data.

<?
if(isset($_POST['width'])){
$width = $_POST['width'];

function mobileView($width){
if ($width < 950){
echo 'true';
} else{
echo 'false';
}
}
mobileView($width);

}else{
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
htmlWidth = $("html").width();
$.ajax({
type: "POST",
url: "mobileView.php",
data:{ width: htmlWidth },
success: function(data){
console.log(data);
}
})
</script>

<?
};

?>

How to read post parameters using JS/Jquery

Client side code has no access to most of the data in the request that was used to load the page the client side code is being executed in, including POST data.

You'll need to use server side code instead.

How to pass parameters in $ajax POST?

I would recommend you to make use of the $.post or $.get syntax of jQuery for simple cases:

$.post('superman', { field1: "hello", field2 : "hello2"}, 
function(returnedData){
console.log(returnedData);
});

If you need to catch the fail cases, just do this:

$.post('superman', { field1: "hello", field2 : "hello2"}, 
function(returnedData){
console.log(returnedData);
}).fail(function(){
console.log("error");
});

Additionally, if you always send a JSON string, you can use $.getJSON or $.post with one more parameter at the very end.

$.post('superman', { field1: "hello", field2 : "hello2"}, 
function(returnedData){
console.log(returnedData);
}, 'json');


Related Topics



Leave a reply



Submit