Handling Form Data - Array in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
devarishi
Forum Contributor
Posts: 101
Joined: Fri Feb 05, 2010 7:15 pm

Handling Form Data - Array in PHP

Post by devarishi »

Hi,


There're two web pages:

1: formData.html
2: processData.php

The formData.html (in browser) web page looks like as follows:

Code: Select all

 
-------------------------------
Case No | Summary | Details
-------------------------------
1           | XYZ       | Blah-Blah!
2           | XYZ       | Blah-Blah!
3           | XYZ       | Blah-Blah!
N           | XYZ       | Blah-Blah!
 
Well, let me give some explanation here: There's a Table to which a new row is dynamically added. Each row consists of 3 cells (text fields in them in fact).

I have no problem with this file.

Now, the crucial part comes when Submit button is clicked and processData.php is called via POST method.

The problem which I am facing is that as the Number of Rows is not predetermined and may vary moreover each Row has 3 Text Fields having unique ID, so, how to handle the Form Data passed to the PHP file in a case like this where we don't know any Element ID or Name in advance?


Note: A code fragment found in the html form for adding a new Row or Item Line:

Code: Select all

 
var newCell1 = newRow.insertCell(1);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'txtRow[]';
  el.id = 'txtRow' + iteration;
  el.size = 20;
  newCell1.appendChild(el); 
 
I think we need to handle Array using the $_POST or $_REQUEST function. Any help, guys?
Last edited by devarishi on Sun Feb 07, 2010 1:38 pm, edited 1 time in total.
cone13cone
Forum Newbie
Posts: 24
Joined: Fri Mar 20, 2009 8:32 pm

Re: Handling Form Data - Arrat in PHP

Post by cone13cone »

Looks like you add input fields to the following columns: Case No Summary Details. You could, name your inputs according to the columns they fall in like el.name = case[], el.name = summary[], and el.name = details[]. By adding the [] on the end you are automatically assigning each input and index. Unless you have a use for the id attribute you don't need to include if you are just submitting data.

On submission, catch the $_POST array and loop through each of the arrays it contains. Such as:

Code: Select all

 
for($i = 0; $i<count($_POST['case']); $i++){
    $case_no = $POST['case'][$i];
    // since you add three inputs to each row we know the lengths of each of your 3 arrays will be the same
    // and $i = 1 row added to the table so the indexes of the inputs on that row will be the same
    $summary = $POST['summary'][$i];
    $details = $POST['details'][$i];
}
 
pretty sure you were asking for something like this.
devarishi
Forum Contributor
Posts: 101
Joined: Fri Feb 05, 2010 7:15 pm

Re: Handling Form Data - Array in PHP

Post by devarishi »

I made this small change in the HTML page:

Code: Select all

el.name = 'Case[]';
And, yes, I liked your confidence! :D You got my problem right in the first place and provided the right approach to resolving it!

Thanks a lot! Moreover, now I also know how to return the number of total elements in an array:

Code: Select all

count();
So, thanks a lot again!

Now I can proceed further. I have to show the data in a neat format and save it in a text file and then send the text file as an attachment in email. (I think I can handle the rest save how to attach a text file which I have to understand.) :wink: Well, I am at the right place so I don't have to worry! Haha!! :drunk:
Post Reply