Retrieving $_POST data question

Discussion in 'PHP' started by NoamBarz, May 9, 2007.

  1. #1
    I have an HTML form that is created by selecting data from a mysql table. The page gets the table name as a variable and creates the form by looping through the table fields (not the values within the fields). I used mysql_fetch_field in order to do this. Displaying the form and validating it, on the client's side, is no problem at all.

    My problem starts when I submit the form. The form action is set to a page "X" and uses the method POST. Is there a way for me to retrieve the POST data without knowing the form field names? I know $_POST is an array, so there must be a way ($_POST[index number] doesn't work).

    In english, what I would like to do is:

    for each field in $_POST fields do something.

    I could, I suppose, retrieve the field names from the mysql table before reading the post data and in this way know what the field names are. However, I'm sure there's a better way. Does anyone know of a solution?
     
    NoamBarz, May 9, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    foreach ((array)$_POST AS $index => $value)
    {
        // $index = Field name
        // $value = Field value
    }
    
    PHP:
     
    nico_swd, May 9, 2007 IP
    commandos likes this.
  3. NoamBarz

    NoamBarz Active Member

    Messages:
    242
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Thanks a lot!! Rep added.
     
    NoamBarz, May 9, 2007 IP
  4. NoamBarz

    NoamBarz Active Member

    Messages:
    242
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #4
    nico_swd's method works like a charm - thanks again.
    However, if by any chance someone is also using input type='file' fields, that person would also have to loop through the FILE array:

    foreach((array)$_FILES AS $index => $value){
    echo $index . " = " . $value['tmp_name'] ."<br>";
    }

    just a tip
     
    NoamBarz, May 9, 2007 IP
  5. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #5
    (array) cast before $_FILES can be removed in case of register_globals = off

    another tip.
     
    gibex, May 10, 2007 IP