Page 1 of 1

how to submit multiple identical form fields w/ php?

Posted: Tue May 26, 2009 1:05 pm
by bmblack
I had a page with multiple forms. I wanted one submit button to submit all the information. I was running into problems because the names of the fields were the same in all the forms. I was told putting all the information into one form and making them arrays would solve my problem. Could somebody explain to me how I would create the php to simply display the information in the code box below?

I'd like it displayed as follows:
Category | Case Number | Description | File Name | File Type
Category | Case Number | Description | File Name | File Type

Code: Select all

<tr><td align="right"><b>Category:</b></td><td>
<select name="category[]">
            <option value="selectone" selected>Select One</option>
            <option value="category1">Category One</option>
            <option value="category2">Category Two</option>
            <option value="category3">Category Three</option>
</select>
</td></tr>
 
<tr><td align="right"><b>Case Number:</b></td><td><input name="casenumber[]" size=40></td></tr>
<tr><td align="right"><b>Description:</b></td><td><input name="description[]" size=40></td></tr>
<tr><td align="right"><b>File Name:</b></td><td><input name="filename[]" size=40></td></tr>
<tr><td align="right"><b>File Type:</b></td><td></td></tr>
<tr><td align="right"><b>Filename:</b></td><td><input type="file" name="file[]" id="file" size=50 /></td></tr>
 
 
<tr><td align="right"><b>Category:</b></td><td>
<select name="category[]">
            <option value="selectone" selected>Select One</option>
            <option value="category1">Category One</option>
            <option value="category2">Category Two</option>
            <option value="category3">Category Three</option>
</select>
</td></tr>
 
<tr><td align="right"><b>Case Number:</b></td><td><input name="casenumber[]" size=40></td></tr>
<tr><td align="right"><b>Description:</b></td><td><input name="description[]" size=40></td></tr>
<tr><td align="right"><b>File Name:</b></td><td><input name="filename[]" size=40></td></tr>
<tr><td align="right"><b>File Type:</b></td><td></td></tr>
<tr><td align="right"><b>Filename:</b></td><td><input type="file" name="file[]" id="file" size=50 /></td></tr>

Re: how to submit multiple identical form fields w/ php?

Posted: Tue May 26, 2009 3:00 pm
by requinix
For the normal fields, $_POST["foo"] will be an array, and the keys inside should exist for the other fields too.
So

Code: Select all

$record = 0; // first record
 
$_POST["category"][$record] // first category
$_POST["casenumber"][$record] // first case number
// etc
The uploaded files are different. The array version of $_FILES looks like

Code: Select all

$_FILES["field name"][$record]["name, tmp_name, etc"]
To get $record use a for loop. It starts at 0 and runs until it $_POST["any field"][$record] isn't set.

Re: how to submit multiple identical form fields w/ php?

Posted: Wed May 27, 2009 2:20 am
by MmmVomit
Like this.

Code: Select all

<form method="post">
<input type="text" name="foo[]" />
<input type="text" name="foo[]" />
<input type="text" name="foo[]" />
<inptu type="submit" />
</form>
If you put square brackets at the end of the field name, the $_POST array will have an element called "foo" that is an array of three values. You can access it like this.

Code: Select all

<?php
 
echo $_POST["foo"][0];
echo $_POST["foo"][1];
echo $_POST["foo"][2];
 
?>