litebearer Posted May 21, 2010 Share Posted May 21, 2010 Regarding the $_POST array. I know how to list all the $_POST array variables; however, I have a couple of questions regarding the output. 1. Are the variables ALWAYS in the same order? (ie does it list the form variables in sequential order - 1st field in form will ALWAYS be 1st $_POST variable, 2nd will be 2nd etc). 2. Is there a way to create a variable named the same as the $_POST variable? (ie take $_POST['address'] and create a variable named $address). Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/202501-creating-variable-names-from-_post-array/ Share on other sites More sharing options...
mcdsoftware Posted May 21, 2010 Share Posted May 21, 2010 Not sure about #1 but to answer #2, see http://php.net/extract. // $_POST = array('address' => '123', 'name' => 'John Doe'); extract($_POST); echo $address; echo $name HTH Quote Link to comment https://forums.phpfreaks.com/topic/202501-creating-variable-names-from-_post-array/#findComment-1061585 Share on other sites More sharing options...
PFMaBiSmAd Posted May 21, 2010 Share Posted May 21, 2010 If you are going to use extract, use the EXTR_PREFIX_ALL parameter and a unique prefix to prevent hackers from overwriting your program variables. Quote Link to comment https://forums.phpfreaks.com/topic/202501-creating-variable-names-from-_post-array/#findComment-1061587 Share on other sites More sharing options...
litebearer Posted May 21, 2010 Author Share Posted May 21, 2010 Interesting (and very possibly part of what I am looking to achieve). Perhaps I need to explaon better what I am trying to do... 1. I am slow poor typist (I type with two fingers while the other 8 sit and watch). 2. I am lazy. 3. The real objective: after creating a form with MANY variables I hate having to retype all the variable names in my form procesing page. I am seeking a way to automatically grab the names and write them to a simpl text file. I can then open the file with all the variable names listed example output (what the file would look like): $address = $_POST['address']; $phone = $_POST['phone']; etc etc Quote Link to comment https://forums.phpfreaks.com/topic/202501-creating-variable-names-from-_post-array/#findComment-1061593 Share on other sites More sharing options...
mcdsoftware Posted May 21, 2010 Share Posted May 21, 2010 Not sure I understand where you're going with this but try: $str = ''; foreach ($_POST as $key => $value) { $str .= '$'. $key .' = $_POST[\''. $key .'\'];' . "\n"; } // write $str to a txt file file_put_contents('file.txt', $str); Quote Link to comment https://forums.phpfreaks.com/topic/202501-creating-variable-names-from-_post-array/#findComment-1061600 Share on other sites More sharing options...
PFMaBiSmAd Posted May 21, 2010 Share Posted May 21, 2010 $_POST is a perfectly fine array variable. In a lot of cases, making other named variables out of every $_POST entry is a waste of processing time, program memory, and typing (both in making the variables and then later referencing all the variables.) If you want to simply store all the form data, you can use a foreach() loop to iterate over the $_POST array. Using the foreach($array as $key => $value) syntax, you can get both the key (index name) and the value. Quote Link to comment https://forums.phpfreaks.com/topic/202501-creating-variable-names-from-_post-array/#findComment-1061602 Share on other sites More sharing options...
litebearer Posted May 21, 2010 Author Share Posted May 21, 2010 Excellent!!!!!!!!!!! Works like a charm! Thank You, Nick... Quote Link to comment https://forums.phpfreaks.com/topic/202501-creating-variable-names-from-_post-array/#findComment-1061604 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.