/    Sign up×
Community /Pin to ProfileBookmark

Retrieving Form data after POST

hi i’m having a little trouble trying to retrieve the data sent in a form. The problem i have is that i want the user to be able to specify data for X amount of rows (the amount of rows are also specified by the user) and then upload them to the database. The text input fields are created by:

[code=php]
$fields = 1;
$count = 0;

while ($count != $no_of_fields)
{

echo ‘<label>’.$fields.’. </label><input type=”text” size=”50″ maxlength=”50″ name=”‘.$fields.'” /><br />’;

$count++;
$fields++;
}[/code]

And my initial thoughts were that as the name of each field would be a number (starting with 1, and in turn field 1 would be accessed by the variable $1 when submitted), i could use a while loop. The while loop would count against the number of rows originally specified by the user to run code to enter each row into the database.

The problem now lies with the naming of the input fields: If the user specifys 5 rows to be added, when the form is submitted the contents of the fields will be stored in variables $1, $2, $3, $4 & $5. Which is fine untill i want to read these in a while loop where i need to vary the variable name. Is that possible? I don’t think it is, so my next plan was to have the contents of each field put into a single array and then use a while loop to read entry one at a time….i’m not to hot on the creation and usages of arrays at the best of times – any one want to point me in the right direction?

Would changing <input type=”text” size=”50″ maxlength=”50″ name=”‘.$fields.'” /> to <input type=”text” size=”50″ maxlength=”50″ name=”array[‘.$fields.’]” /> cause the results to be put in $array?

Cheers, Durbs

P.S. Sorry if that makes no sense what-so-ever…

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@DurbsauthorAug 08.2005 — OOOOOOPS....sorry guys - i've done it now, the array thing worked. Clever stuff this PHP...... :eek:
Copy linkTweet thisAlerts:
@bokehAug 08.2005 — Hi! I'm sure you are trying to do something like this:
[code=php]for ($i = 1; $i <= $fields; $i++) {
echo '<input type="text" size="50" maxlength="50" name="fields[]" />';
}[/code]


Now, when the form is submitted $_POST['fields'] will contain a numerically keyed array.
Copy linkTweet thisAlerts:
@DurbsauthorAug 08.2005 — Well this is what i did:
[code=php]$count = 0;
$fields = 1;
while ($count != $no_fields)
{
echo '<label>'.$fields.'. </label><input type="text" size="50" maxlength="50" name=fields['.$count.'] /><br />';

$count++;
$teams++;
}[/code]

Does name="fields[]" do the same as name=fields['.$count.']. Also should i be using a for loop instead of while loop? Whats the advantages?

cheers, Durbs
Copy linkTweet thisAlerts:
@bokehAug 08.2005 — Does name="fields[]" do the same as name=fields['.$count.']. Also should i be using a for loop instead of while loop? Whats the advantages?

cheers, Durbs[/QUOTE]


A 'for' loop seems more natural to me here just a screwdriver rather than a hammer seems more natural for inserting screws. One thing is for sure though, your method looks more complicated.

If $count started at '0' then name="fields[]" and name="fields['.$count.']" would produce identical $_POST['field'] arrays but not if you started on a number other than zero.

Also with your method you could enter the numbers into the array no sequentially.
Copy linkTweet thisAlerts:
@Stephen_PhilbinAug 08.2005 — You can use either loop. While's are generally used for more simple stuff, whilst for's are for more complex things.

Have a look at this here: http://www.zend.com/manual/control-structures.for.php
×

Success!

Help @Durbs spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...