Jump to content

[SOLVED] Receiver GET and/or POST vars with javascript


therealwesfoster

Recommended Posts

you can use basic JavaScript for GET method; something like this:

 

<script language="javascript">
var get = document.location.search.substring(1);
document.write(""+get+"");
</script>

 

but your probably going to have to use AJAX to get your POST method; off the top of my head I cannot think of a way to get the POST method with basic JavaScript.

Link to comment
Share on other sites

Here is a code segment from my mootools extenssion that grabs the get variables and generate an array $_GET (the same as php $_GET)

 

var $_GET = new Array;

// internal script to parse the $_GET variable
var _uri = location.href;
var _temp_get_arr = _uri.substring(_uri.indexOf('?')+1, _uri.length).split("&");
var _temp_get_arr_1 = new Array();
var _temp_get_val_holder = "";
for(_get_arr_i=0; _get_arr_i<_temp_get_arr.length; _get_arr_i++){
_temp_get_val_holder = "";
_temp_get_arr_1 = _temp_get_arr[_get_arr_i].split("=");
for (_get_arr_j=1; _get_arr_j<_temp_get_arr_1.length; _get_arr_j++){
	if (_get_arr_j > 1) _temp_get_val_holder += "=";
	_temp_get_val_holder += decodeURI(_temp_get_arr_1[_get_arr_j]);
}
$_GET[decodeURI(_temp_get_arr_1[0])] = _temp_get_val_holder;
}
delete _uri; delete _temp_get_arr; delete _temp_get_arr_1; delete _temp_get_val_holder;

 

You can't get post variables from JS, use a php function to generate it in your page

 

<?PHP
echo "<script language=\"javascript\">
\$_POST = new Array()";
if (count($_POST) > 0){
    foreach ($_POST as $k=>$v){
        echo "\$_POST['$k'] = '$v';\n";
    }
}
echo "</script>";
?>

  Not tested

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.