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 Link to comment https://forums.phpfreaks.com/topic/80032-solved-receiver-get-andor-post-vars-with-javascript/ 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. Link to comment https://forums.phpfreaks.com/topic/80032-solved-receiver-get-andor-post-vars-with-javascript/#findComment-405557 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 Link to comment https://forums.phpfreaks.com/topic/80032-solved-receiver-get-andor-post-vars-with-javascript/#findComment-408364 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 Link to comment https://forums.phpfreaks.com/topic/80032-solved-receiver-get-andor-post-vars-with-javascript/#findComment-408686 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.