whiterecluse Posted December 20, 2006 Share Posted December 20, 2006 Is there an easy way to retrieve all of the variables from a form? Using $_POST for every variable would take a lot of code for what I wanna do. I'm hoping theres a way I can do it by using this code or a variation of it: [code]foreach($HTTP_POST_VARS as $key => $val){ //do something with the val....}[/code]Pointing me to any tutorials would be great too :) Link to comment https://forums.phpfreaks.com/topic/31430-php-and-form-variables/ Share on other sites More sharing options...
matto Posted December 21, 2006 Share Posted December 21, 2006 That's the right idea, but use $_POST rather than $HTTP_POST_VARS... :) Link to comment https://forums.phpfreaks.com/topic/31430-php-and-form-variables/#findComment-145533 Share on other sites More sharing options...
JasonLewis Posted December 21, 2006 Share Posted December 21, 2006 that would select EVERYTHING in the $_POST array, is that what your wanting to do?(damn! people always beat me!:)) Link to comment https://forums.phpfreaks.com/topic/31430-php-and-form-variables/#findComment-145538 Share on other sites More sharing options...
whiterecluse Posted December 21, 2006 Author Share Posted December 21, 2006 I want the equivalent of [code]$var1 = $_POST['var1'];$var2 = $_POST['var2'];$varX = $_POST['varX'];[/code]Only through a loop. Is there a way to do this? Link to comment https://forums.phpfreaks.com/topic/31430-php-and-form-variables/#findComment-145888 Share on other sites More sharing options...
kenrbnsn Posted December 21, 2006 Share Posted December 21, 2006 You want to use the concept of [url=http://www.php.net/manual/en/language.variables.variable.php]variable variables[/url]:[code]<?phpforeach($_POST as $key => $value) $$key = $value;?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/31430-php-and-form-variables/#findComment-145891 Share on other sites More sharing options...
whiterecluse Posted December 21, 2006 Author Share Posted December 21, 2006 That's what I thought I needed to use but I can't seem to grasp the concept. $$key = $val;does this mean that a variable named $val1 will be stored in $$key?What I want is to be able to compare the value of one variable against the value of another using variable variables (I think I would use them) Link to comment https://forums.phpfreaks.com/topic/31430-php-and-form-variables/#findComment-145897 Share on other sites More sharing options...
HuggieBear Posted December 21, 2006 Share Posted December 21, 2006 If you're form had two fields, [i]username[/i] and [i]realname[/i] and I submitted the form with my details (HuggieBear and Richard) then PHP would get the following variables:[code=php:0]$_POST['username']$_POST['realname'][/code] If you want a these to be variables with the same name as the key, then do this...[code]<?phpforeach($_POST as $key => $value) $$key = $value;?>[/code]This means that if I want to echo Richard I can type [code=php:0]echo $realname;[/code] not [code=php:0]echo $$key;[/code] The above code has created variables with the names of the keys of the $_POST array.RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/31430-php-and-form-variables/#findComment-145914 Share on other sites More sharing options...
whiterecluse Posted December 21, 2006 Author Share Posted December 21, 2006 You're awesome! That's what I wanted ;D Link to comment https://forums.phpfreaks.com/topic/31430-php-and-form-variables/#findComment-145916 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.