severndigital Posted March 8, 2007 Share Posted March 8, 2007 I am going through my exisiting code for a new version of my application. I need to process the results of form fields currently I do it like this. $var1 = $_POST['field1']; $var2 = $_POST['field2']; I don't mind this method except that I have to process 15 - 20 fields on each submit. So, I have to cut and paste this code over and over again and modify the vars and field names for each submit. is there an easy way to process the fields and assign variables to them, based on the name of the field processed? I am thinking I should use a foreach statement, but I am not sure exactly how to use it properly or if it can automatically assign field names and values. I supposed I could run an sql query to get the proper field names and then build the post statements dynamically based off of that information. Anyway .. any thoughts or suggestions on this one, would be much appreciated. Thanks, Chris Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 8, 2007 Share Posted March 8, 2007 If you can change the form, change the names of the fields to arrays, then you can use a "for" loop. Ken Quote Link to comment Share on other sites More sharing options...
severndigital Posted March 8, 2007 Author Share Posted March 8, 2007 I can change pretty much anything I need to. I am reworking my own existing code for a vesion upgrade on an application I designed. Can you explain the process in more detail? thank you. chris Quote Link to comment Share on other sites More sharing options...
Glyde Posted March 8, 2007 Share Posted March 8, 2007 Look below. Your HTML form would look something like this: <form action="mypage.php" method="post"> <input type="text" name="field[]" /> <input type="text" name="field[]" /> <input type="text" name="field[]" /> <input type="submit" name="submit" value="Submit" /> </form> Your PHP page would look something like this: <?php if ( isset( $_POST['submit'] ) ) { foreach ( $_POST['field'] as $key=>$val ) { "Field: $key has a value of '$val'"; } } ?> Quote Link to comment Share on other sites More sharing options...
severndigital Posted March 9, 2007 Author Share Posted March 9, 2007 Ok, i've grasped the idea of using for each to get the variables from the $_POST command. But how do I incorperate them into a DB Query? assume for this that I am auto collecting username and password, then I would like to use them in a mysql query here's what i have so far: <? //grab all fields from the form foreach($_POST['field'] as $key => $val){ } //I know the mysql query would like this. $user_check = mysql_query("SELECT * FROM users WHERE username='$whatgoeshere' AND password='$whatgoeshere'"); ?> I just don't know how to get the form fields to interact with other code together. the foreach loop seems to want to make them work independently of each other. Is what I want to do here possible? or should I just be hard coding this. The above is one example. I would also like to put this in place for updating information in the db and also passing the information through multiple forms. any help would be great. thanks, chris 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.