scottwhittaker Posted April 30, 2010 Share Posted April 30, 2010 Hi Is it posible to use form field names as variable names like so... <form name="test" method="GET"....> <input name="surname">.... when submitted creates URL like form_handler.php?surname=value_entered Can I somehow set a variable with the name of each form input and set it to the value entered so that when I type "print $surname;" It prints the name name they entered? I know this sounds strange but I want to create a script for use on many different forms which I wont know all of the input names for. Hope that makes sense, kinda hard to explain though. I've had a look but nothing came up. Cheers Scott Quote Link to comment https://forums.phpfreaks.com/topic/200286-form-field-names-as-variable-names/ Share on other sites More sharing options...
Ken2k7 Posted April 30, 2010 Share Posted April 30, 2010 $surname = $_GET['surname']; Or better yet: $surname = filter_input(INPUT_GET, 'surname'); Like that. But is there something wrong with $_GET or filter_input? If you need to keep data across different files, use $_SESSION. Quote Link to comment https://forums.phpfreaks.com/topic/200286-form-field-names-as-variable-names/#findComment-1051092 Share on other sites More sharing options...
scottwhittaker Posted April 30, 2010 Author Share Posted April 30, 2010 Hi thanks for the response but thats not what I was meaning although I see how u thought that. What you replied with was how to put a POST or GET value into a variable BUT what if I do not know the name of the input field which would mean I cannot type the code you supplied because I do not know the name of the bit in red...here... ( $_GET['surname']; ). I want the variable name to be the name of the input field (almost like a dynamically created variable name) and the value of the variable to be the value entered on the form. I could swear I had read about this somewhere previously but its looking like I havent! Quote Link to comment https://forums.phpfreaks.com/topic/200286-form-field-names-as-variable-names/#findComment-1051263 Share on other sites More sharing options...
947740 Posted April 30, 2010 Share Posted April 30, 2010 $_GET[$$variable] Quote Link to comment https://forums.phpfreaks.com/topic/200286-form-field-names-as-variable-names/#findComment-1051283 Share on other sites More sharing options...
Ken2k7 Posted May 1, 2010 Share Posted May 1, 2010 If that's the case, let me see if I get this, you know the variable names but not the input field names? WTF? Quote Link to comment https://forums.phpfreaks.com/topic/200286-form-field-names-as-variable-names/#findComment-1051429 Share on other sites More sharing options...
scottwhittaker Posted May 1, 2010 Author Share Posted May 1, 2010 Ha ye! When you put it like that it sounds stupid but if theres a lot of inputs and therefore subsequent variables, something like this would have saved a lot of this.... $variable = $_GET['input_name']; Quote Link to comment https://forums.phpfreaks.com/topic/200286-form-field-names-as-variable-names/#findComment-1051515 Share on other sites More sharing options...
Axeia Posted May 1, 2010 Share Posted May 1, 2010 I think it's possible using variable variables . As you may have guessed from the name, they just add to the confusion and are generally considered bad practice. Stick to using $_GET. Quote Link to comment https://forums.phpfreaks.com/topic/200286-form-field-names-as-variable-names/#findComment-1051542 Share on other sites More sharing options...
947740 Posted May 1, 2010 Share Posted May 1, 2010 I think it's possible using variable variables . As you may have guessed from the name, they just add to the confusion and are generally considered bad practice. Stick to using $_GET. Hence my post. Although, I must concede I did not give near the explanation you did. Quote Link to comment https://forums.phpfreaks.com/topic/200286-form-field-names-as-variable-names/#findComment-1051559 Share on other sites More sharing options...
Alex Posted May 1, 2010 Share Posted May 1, 2010 register_globals does this, but it's depreciated and a huge security risk, so I do not suggest you use this. As for the code you're after, I think you want something like this: foreach($_GET as $var => $val) { $$var = $val; } But again, this poses the same security flaw as register_globals, because anyone can inject their own variables into your script, so again I do not suggest you use this. Quote Link to comment https://forums.phpfreaks.com/topic/200286-form-field-names-as-variable-names/#findComment-1051565 Share on other sites More sharing options...
PFMaBiSmAd Posted May 1, 2010 Share Posted May 1, 2010 The $_GET array is a perfectly usable set of variables. By making a series of named variables from it, you must now keep track of each of those variable names and know how many there were and write out code to reference each one. That does not make for general purpose coding. script for use on many different forms I wont know all of the input names for Since you are trying to write a generic script where you won't know the names before hand, it will be impossible for you to know how many and what names the variables will be. Just use array functions (i.e. foreach(){}) to process the $_GET array directly without going through an additional intermediate step of creating named variables (this will save processing time and memory as well.) Quote Link to comment https://forums.phpfreaks.com/topic/200286-form-field-names-as-variable-names/#findComment-1051567 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.