SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    164
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Process unknown number of form fields

    Hi all,

    I'm having a bit of trouble processing a dynamic expenses form.

    In the form I have 2 buttons that increment the number of form fields in 2 different places on the page.

    I named the form elements according to the table field in the database.

    If the form field is multiplied (clicking the add form field button), I add an auto_increment number for the id with javascript:

    txtData1 ->(original form element)
    txtData2 ->(created dynamically)
    txtData3 ->(created dynamically)
    txtData4 ->(created dynamically)
    etc

    When processing the form, I know I can get the formfield names and values using:

    Code PHP:
    foreach ($_POST as $key => $value) 
    {  
    		echo $key ."->". $value."<br>"; 
    }


    But I need to collect groups of information (form fields) from the page and insert into a expenses database.

    So the majority of the fields will go into one table, but other will go into another.

    How can I differentiate the necessary keys and build the query?

    Hope I explained my self right...

    Thanks in advance.
    Yours truely
    Mário Ramos

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I had to write the code I would have used field arrays instead of auto incremented, but since you already have a working code lets improvise.
    PHP Code:
    $txtData = array();
    foreach ( 
    $_POST as $key => $value ) { 
        if ( 
    strpos$key"txtdata" ) ) {
            
    $idx substr$key);
            
    $txtdata[$idx] = $value;
        }

    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    164
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Hi

    Thanks for the reply...
    I'll give it a try.
    Yours truely
    Mário Ramos


Bookmarks

Posting Permissions

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