stig1 Posted November 24, 2010 Share Posted November 24, 2010 I have a form with lots of fields. How would I run a loop to get each field name and data into an array, so i can then send it off to a function to process each field name / data differently. Instead of typing all the variables in the main function name for each post. Would a foreach statement work? Quote Link to comment https://forums.phpfreaks.com/topic/219655-post-array-form-data/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 24, 2010 Share Posted November 24, 2010 Your form data is already in an array, either $_POST or $_GET, depending on the form's method="" attribute. You should however have a master array that contains a list of the expected field names so that you can iterate over the expected fields and detect/ignore any that a bot script or hacker is attempting to send that are not part of your actual form. Quote Link to comment https://forums.phpfreaks.com/topic/219655-post-array-form-data/#findComment-1138843 Share on other sites More sharing options...
stig1 Posted November 24, 2010 Author Share Posted November 24, 2010 i pretty sure i worked it out, i wrote the following: $kv = array(); foreach ($_POST as $key => $value) { $kv[$key] = "$value"; } to pull the data out i just use the $kv["keyname"] and it seems to work! Quote Link to comment https://forums.phpfreaks.com/topic/219655-post-array-form-data/#findComment-1138847 Share on other sites More sharing options...
Unirawan Posted November 24, 2010 Share Posted November 24, 2010 the $_REQUEST array will contain passed $_POST and $_GET data regardless of the method declaration. as to protection, use a challenge-response mechanism protection (like captcha/reCaptcha) to prevent automation through your forms. I do not recommend session tickets in hidden form values because they can be bypassed automatically by parsing the html for the hidden values. Quote Link to comment https://forums.phpfreaks.com/topic/219655-post-array-form-data/#findComment-1138848 Share on other sites More sharing options...
Pikachu2000 Posted November 24, 2010 Share Posted November 24, 2010 I guess you could use that foreach() loop to do that, but it would be identical to assigning it directly, like $kv = $_POST. What would be the point in looping through one array ($_POST) simply to assign its values to another identical array, when you can just use them directly? Quote Link to comment https://forums.phpfreaks.com/topic/219655-post-array-form-data/#findComment-1138850 Share on other sites More sharing options...
Unirawan Posted November 24, 2010 Share Posted November 24, 2010 agreed - it's redundant. each $_REQUEST['keyname'] can be accessed directly. Quote Link to comment https://forums.phpfreaks.com/topic/219655-post-array-form-data/#findComment-1138855 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.