...

View Full Version : looping through form variables php



nickyfraggle
01-01-2008, 09:21 PM
Hello,

I'd like to loop through a list of form elements posted to my php page, and insert their values into the database.

The form page contains a list of input fields, and is dynamic i.e. a back office allows them to add more information and therefore increase the list of inputs on my form page.

So what I think I need to do is loop through the posted values...assign them a name (which is be the name of the field they were posted from)...and then insert them into the database. If a field is blank...I don't want it to be inserted...I only want to insert fields with something in them.

Each posted value is added to the database seperately. They are parts.

So, in my database, I store the part name, the ID (to tell me where it came from) and the posted value. The part name is the name of the field that the value was posted from. SO...I'd need to store the name of the field it was posted from...together with the value within that field...and insert them both into the database together (but into seperate fields!)

I think this could prob be done each time the loop went through and got the posted values & name of text field. Each time it looped through it would get the input name, it's posted value and insert it into the database.

Anyone have any ideas? That's if I haven't confused you completely....it's pretty much scrambled my brain just trying to get it all down on paper for you!

Thanks!

Nicola

nickyfraggle
01-01-2008, 09:22 PM
I'm going to keep having a fiddle so I might post some of my attempts soon!

Thanks!

oesxyl
01-02-2008, 03:38 AM
this was my solution for someting close to what you need:

http://www.codingforums.com/showpost.php?p=643075&postcount=2

- common prefix for the name of the html form element, something as



<input type="text" name="dyn...."


- filter from $_POST only the $_POST['dyn...' elements retrive slice in one array
- check value in resulted array if are valid, empty fields I think are not in the $_POST, but if I'm wrong you could remove at this step
- construct the query as you need processing the

I do this some times, it work but I never have radio and checkbox in the form.
Anyway the script must be tested in many situation because are many thing what could go wrong in this chain.

best regards

hammer65
01-02-2008, 09:29 PM
In order to get the data from the form to the database in a secure manner, and ensure that the data is what you want in your database, you need to validate (client side for convenience and less server load, server side for security. Do both) the data, provide a way for the user to correct mistakes, filter data to match what you want to see in your data, escape input for security (or use prepared statements), insert the data into the database.

There are many tutorials available that cover those steps. In this case Google is your friend.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum