hendricksonHelp
asked on
How to dynamically get form names in a PHP $Post
I am writing a page that takes a user's data from a database and displays it in a web page. Then the user is given the option to update or delete portions of the information. I am having an issue on the PHP side, after the user has submitted the form. Here is an example of the form being submitted by the user.
Okay. The important thing to note here is that the name for each text box is dynamically generated via php. So it will never be the same. It will always start out as "item", but the number is the ID number of the item stored in the database. Also, the number of input items may be different. Sometimes there may be 5 textboxes, other times there could be 20.
I am wondering if there is a way to a list of names that were posted without using a hidden field. Maybe something like $_Post[0].name(), where the zero is the first item in the post array, and the function name() would give me the name of the posted input.
I don't know, as long as I don't have to write an entire novel's worth of code to get it to work would be good. I am also new to php, so keep that in mind.
<form action="submit.php" method="post">
<input name="item4" type="text" />
<input name="item75" type="text" />
</form>
Okay. The important thing to note here is that the name for each text box is dynamically generated via php. So it will never be the same. It will always start out as "item", but the number is the ID number of the item stored in the database. Also, the number of input items may be different. Sometimes there may be 5 textboxes, other times there could be 20.
I am wondering if there is a way to a list of names that were posted without using a hidden field. Maybe something like $_Post[0].name(), where the zero is the first item in the post array, and the function name() would give me the name of the posted input.
I don't know, as long as I don't have to write an entire novel's worth of code to get it to work would be good. I am also new to php, so keep that in mind.
ASKER
I thought about doing that, however, the issue comes in when I have 500 or 5000 entries in the entire database. If it the first entry uses ID number 4000 and the last entry uses ID number 5000 I would have to loop through 1000 numbers to access each item individually...or would I? Like I said, I am not familiar with php.
No you don't,
you can do an foreach loop:
you can do an foreach loop:
foreach ($_POST['item'] as $key => $value) {
...
}
ASKER
Ok, so I will break this down, and you tell me if I am correct.
In "$_Post['item']" the item is the array. If I had named the input as "Suzy[75]" then I would replace the post as "$_Post['Suzy']"
and the $key => $value....I have no idea what that is. Does it change?
Basically,I have a function called verify(str).
The verify function takes a string argument, and checks that string for illegal characters. It returns false if no illegal characters were found, or true if there were illegal characters.
how would I use that function with this foreach statement?
In "$_Post['item']" the item is the array. If I had named the input as "Suzy[75]" then I would replace the post as "$_Post['Suzy']"
and the $key => $value....I have no idea what that is. Does it change?
Basically,I have a function called verify(str).
The verify function takes a string argument, and checks that string for illegal characters. It returns false if no illegal characters were found, or true if there were illegal characters.
how would I use that function with this foreach statement?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I will test this later today, and award points. Thanks.
Open in new window
Then you can in php retreive all the values like an array:
Open in new window