LiamProductions Posted August 10, 2007 Share Posted August 10, 2007 Hey. Is there a quicker of recieving all data for people with register globals off? because if you have a big form with like 100 questions im sure people would'nt want to write 100 same lines of code to recieve each piece of data to the page then achually having to code the final result. Is'nt there a quicker way with one line or code or something to recieve all the data? Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 10, 2007 Share Posted August 10, 2007 if by "recieve [sic] the data" (i before except after which letter?) you mean localize the variables, extract() will do this for you. alternatively, you can simply run a foreach() with dynamic variable generation: foreach ($_POST AS $key => $val) { $$key = $val; } i don't see the point in achually (i don't know what the 'h' is doing there, was 't' out for a pint?) localizing the variables, as this really doesn't save you too much time. it also decreases your flexibility in dealing with multi-dimensional arrays and automating your query strings. Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted August 10, 2007 Author Share Posted August 10, 2007 Can you explain how i would use the extract() function because i've looked at php.net/extract i don't really understand it. Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 10, 2007 Share Posted August 10, 2007 it's quite simple. the manual says this: It takes an associative array var_array and treats keys as variable names and values as variable values. check the example code snippet, which tells you EXACTLY how it works. Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted August 10, 2007 Author Share Posted August 10, 2007 so if i put <?php extract('formdata.php'); ?> that would get all the data from formdata.php? Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 10, 2007 Share Posted August 10, 2007 .. no, extract() expects an array. if you pass extract the $_POST array, it will pull all the $_POST variables into the local namespace using their keys as variable names. perhaps i've misunderstood - what exactly are you asking? Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted August 10, 2007 Author Share Posted August 10, 2007 What i mean is if i post like 100 different data forms like a text field 100 different ones naming them like field name, address ect... i would have to write 100 different lines to get the data to the next page. I want to know if theres a quicker way to recieve all the data on the page. A by pass through register globals or something. Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 10, 2007 Share Posted August 10, 2007 ... i would have to write 100 different lines to get the data to the next page. do you mean this sort of thing: $fieldname1 = $_POST['fieldname1']; if so, then use extract(), as i suggested. Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted August 10, 2007 Author Share Posted August 10, 2007 ... i would have to write 100 different lines to get the data to the next page. do you mean this sort of thing: $fieldname1 = $_POST['fieldname1']; if so, then use extract(), as i suggested. Yes the code you just put is what i mean... but i want to know how to extract all the codes would it be something like $alldata = extract($_POST['ALL_FIELDS']; That code is probly wrong. but im hoping you can see what i want happening now, Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 10, 2007 Share Posted August 10, 2007 why not try things before asking if something will work? your example clearly won't, but we're not here to spoon-feed you the answers - you can always give it a shot, see if it works, and if it doesn't, look around to figure out what's wrong rather than asking as a first resource. a good programmer knows how to seek a simple answer to a simple question: extract($_POST); voila. Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted August 10, 2007 Author Share Posted August 10, 2007 I'm not even doing anything with forms Just wanted to know how to do it Quote Link to comment Share on other sites More sharing options...
Guest Posted August 11, 2007 Share Posted August 11, 2007 @akitchin Sorry, but I'm going to try to simplify what you've been trying to explain. Correct me if I'm wrong. @LiamProductions Simply said, lets say you have two textfields, named "username" and "password" Once submitted, they can be retrieved using: $_POST['username'] and/or $_POST['password'] Now, if you use extract like akitchin has demonstrated: extract($_POST); after you run that line of code, username and password will become available using: $username and/or $password And there you have it! Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted August 11, 2007 Author Share Posted August 11, 2007 I understand now. 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.