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
bmblack
Forum Newbie
Posts: 3 Joined: Fri May 22, 2009 8:26 am
Post
by bmblack » Tue May 26, 2009 1:05 pm
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>
Last edited by
Benjamin on Tue May 26, 2009 4:25 pm, edited 2 times in total.
Reason: Changed code type from text to php.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue May 26, 2009 3:00 pm
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 .
MmmVomit
Forum Newbie
Posts: 4 Joined: Wed May 27, 2009 1:09 am
Post
by MmmVomit » Wed May 27, 2009 2:20 am
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];
?>
Last edited by
Benjamin on Wed May 27, 2009 9:56 am, edited 1 time in total.
Reason: Changed code type from text to php.