therealwesfoster Posted December 3, 2007 Share Posted December 3, 2007 I've search around but can't find a way to do this.. Im wanting to grab a GET variable from a form and use it within my script. How would I do this? And since im here, how would I get POST vars as well? Thanks Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted December 3, 2007 Share Posted December 3, 2007 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. Quote Link to comment Share on other sites More sharing options...
nogray Posted December 6, 2007 Share Posted December 6, 2007 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 Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted December 7, 2007 Author Share Posted December 7, 2007 Very nice function there nogray I appreciate it Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.