/    Sign up×
Community /Pin to ProfileBookmark

Variable name from a from cycle

Hi,
i’m trying to retrieve data from a form with this code:

[CODE]<?php
for ($i=1; $i<=7; $i++)
{
$dat.$i = $_POST[“dat”.$i];
echo $dat.$i;
}
?>[/CODE]

but it doesn’t work, if i skip the variable doing so:

[CODE]<?php
for ($i=1; $i<=7; $i++)
{
echo = $_POST[“dat”.$i];
}
?>[/CODE]

i get correctly all the results, but putting them in a variable the result is just the first value (dat1)

For any doubt, this is the form code:

[CODE]<form action=”date_insert_check.php” method=”post” >
<?
for ($i=1; $i<=7; $i++)
{
?>
<? echo $i ?>: <input name=”dat<? echo $i ?>” type=”radio” value=”2010-04-<? echo $i ?>”>
<?
}
?>[/CODE]

<input type=”submit” name=”Submit” value=”Submit”>
</form>

Thanks a lot for any suggestion.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 13.2010 — I would recommend using the "[]" array notation for your form field names.
[code=php]
<input name="dat[<?php echo $i ?>]" type="radio" value="2010-04-<?php echo $i ?>">
[/code]

Then the values will be in $_POST['dat'][1], $_POST['dat'][2], etc. In the form-handler script you could then do a foreach() loop on that sub-array and assign it to its own array, if that's really necessary:
[code=php]
$dat = array();
foreach($_POST['dat'] as $ix => $value) {
$dat[$ix] = $value;
}
[/code]
Copy linkTweet thisAlerts:
@JackbobauthorApr 15.2010 — Thanks a lot, grat and simple solution
×

Success!

Help @Jackbob 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.19,
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,
)...