/    Sign up×
Community /Pin to ProfileBookmark

$_POST in loop?

Will this not work (well, obviously it doesn’t or I wouldn’t be inquiring.) I need to call variable created in a previous form but don’t know how many I have until it is carried over. I have an input in my previous showing how many there are in the file. Now I’m trying to do this but it doesn’t produce anything.

[code=php]
for ($i=0;$i<=$num_of_lines;$i++)
{
$checkbox[$i] = $_POST[‘checkbox[$i]’];
}
[/code]

This won’t even work for me.

[code=php]
for ($i=0;$i<=$num_of_lines;$i++)
{
$checkbox[$i] = $_POST[‘checkbox$i’];
}
[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 10.2005 — Name your checkbox items in your form using the '[]' technique so that PHP recognizes them as array elements:
<i>
</i>&lt;p&gt;&lt;input type=checkbox name="check[]" value=1&gt;One&lt;/p&gt;
&lt;p&gt;&lt;input type=checkbox name="check[]" value=2&gt;Two&lt;/p&gt;
&lt;p&gt;&lt;input type=checkbox name="check[]" value=3&gt;Three&lt;/p&gt;
&lt;p&gt;&lt;input type=checkbox name="check[]" value=4&gt;Four&lt;/p&gt;

The PHP form-handler will now have an array $_POST['check'] populated for each selected checkbox, which you can just foreach() your way through:

[code=php]
echo "<p>You selected:n<ul>n";
foreach($_POST['check'] as $value)
{
echo "<li>$value</li>n";
}
echo "</ul>n";
[/code]
Copy linkTweet thisAlerts:
@ephmynusauthorMar 10.2005 — spasibo
×

Success!

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