Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts

    looping through form variables php

    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

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts
    I'm going to keep having a fiddle so I might post some of my attempts soon!

    Thanks!

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    this was my solution for someting close to what you need:

    problem with update with multiple textbox

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

    Code:
    <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

  • #4
    Regular Coder
    Join Date
    Dec 2007
    Location
    Nebraska
    Posts
    113
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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.
    Deliver yesterday, code today, think tomorrow.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •