Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Processing an arbittrary number of form fields

Status
Not open for further replies.

theEclipse

Programmer
Dec 27, 1999
1,190
US
Hello....

I am wondering what other peoples soloutions to this problem are, as I am going to have to come up with something....[lol]:

I am scaning a directory and pulling out all the files from the directory. Then I am creating a form that has a table row for each file. For each file I have an 2 actions to perform and a description that will be entered for that file. (in essence I have 4 or 5 form fields for each file in the directory, which will have n files in it.)

As of right now, the script prints out the file number as a prefix to each form field. ex "0name" "0descript" ect.

The only solution that I can come up with is something like this:

a counter while loop that checks the post to see if "0name" is there....process the rest of the fields for that file, increment counter.....

Any other ideas?



Robert Carpenter
"You and I have need of the strongest spell that can be found to wake us from the evil enchantment of worldliness." - C.S. Lewis (The Weight of Glory)

robert (at) robertcarpenter (dot) net
 
Name all your form fields similarly to:

<input type="text" name="foo[]"...>

That way, when the form is submitted, $_POST['foo'] (or $_GET['foo']) will itself be an array.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
sleipnir -

I did think about that....then I would have an array of names, an array of actions, descriptions, ect.

Is there a way to make it so that when the form is submitted I get one array, with each element being an object?

ex:
<input type="text" name="foo[n]->name" value="mike">
<input type="text" name="foo[n]->desc" value="wierdo">
<input type="text" name="foo[n+1]->name" value="cherry">
<input type="text" name="foo[n+1]->desc" value="red">
the the array would look something like this:

Array[0]{
name->mike
desc->wierdo
}
Array[1]{
name->cherry
desc->red
}


I tried the above syntax, but it doesnt work. My thought is that php doesnt know what class that would be.

Any ideas?

Robert Carpenter
"You and I have need of the strongest spell that can be found to wake us from the evil enchantment of worldliness." - C.S. Lewis (The Weight of Glory)

robert (at) robertcarpenter (dot) net
 
As an object? No way. You could format it to get out a multidimentional array.

Pointing my browser to test_multipost.html:
Code:
<html><body>
<form method="post" action="test_multipost.php">
<input type="text" name="entry[0][name]"><input type="text" name="entry[0][address]"><br>
<input type="text" name="entry[1][name]"><input type="text" name="entry[1][address]"><br>
<input type="text" name="entry[2][name]"><input type="text" name="entry[2][address]"><br>
<input type="submit">
</form></body></html>

filling in the fields and submitting to test_multipost.php:
Code:
<?php

print '<html><body><pre>';

print_r ($_POST);

print '</pre></body></html>';

?>

outputs:

Code:
Array
(
    [entry] => Array
        (
            [0] => Array
                (
                    [name] => a
                    [address] => 123
                )

            [1] => Array
                (
                    [name] => b
                    [address] => 456
                )

            [2] => Array
                (
                    [name] => c
                    [address] => 789
                )

        )

)


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
One "gotcha" with doing this. Client-side JavaScript will not like these object names, so JavaScript code will not be able to manipulate their values.

But there is a workaround. If you use input tags of the form:

<input type="text" name="foo[0][name]" id="foo_0_name"....>

Then you can refer to the object by the id in your JavaScript code.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
interesting.....thanks for the ideas guys (or girls).

I decided that I am going to be using mulitple arrays idea first presented.

Excerpt from the form output loop (this gets run once for each file in the directory)
Code:
print <<<END
<input type="hidden" name="names[]" value="{$fileObj->fileName}" />
<table border="0">
<tr><td>File: "{$fileObj->displayName}"</td></tr>
<tr><td>
<select name="actions[]">
END;
$selectedText = "";
for($i=0; $i<count($fileActions); $i++){
	if ($i==2) $selectedText = "selected=\"selected\" ";
	else $selectedText = "";
	echo "<option value=\"".($i-1)."\" {$selectedText}>$fileActions[$i]</option>\n";
}
print <<<END
</select>
</td></tr>
</table>


Most of the form processing script. The searchFileList function just searches my array of objects for a specific file object.
Code:
if (isset($DI_submit)){
	$i=-1;
	while (isset($DI_names[++$i])){
		if (($DI_actions[$i]>0)
		    &&(false !== ($fileO = searchFileList($fileList,$DI_names[$i])))
		   )
		   	copy($configData["uploadDir"] . $DI_names[$i], $configData["downloadDir"] . $DI_names[$i]);
		   	unlink($configData["uploadDir"] . $DI_names[$i]);
	//add sql insert here
		   {

		}
	}
}

I suppose that I am not really sure that is all of a "complete" script.....I am just posting for completeness.

Thanks again for bouncin ideas....

Robert Carpenter
"You and I have need of the strongest spell that can be found to wake us from the evil enchantment of worldliness." - C.S. Lewis (The Weight of Glory)

robert (at) robertcarpenter (dot) net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top