/    Sign up×
Community /Pin to ProfileBookmark

Get POST status of all items

I’m displaying all items from a database on my form with a checkbox assigned to it. Allowing users to select as many items from my collection as possbie.

On my form submit, how can I collect all those selected checkboxes?

Normally it would be $_POST[‘item1’]; but I can’t hardcode each item that’s being posted as they’re coming from my database.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 12.2020 — One possibility is to use "array naming" in the HTML form names, perhaps using the ID from the DB for each?
<i>
</i>foreach($db_rows as $row) { // assume it includes $row['id'] and $row['name']
echo "&lt;p&gt;&lt;input type='checkbox' name='foo[{$row['id']}]' value='{$row['name']}'&gt; {$row['name']}&lt;/p&gt;n";
}

Then you could process whichever get submitted (since only checked checkboxes actually submit):
<i>
</i>foreach($_POST['foo'] as $id =&gt; $name) {
// do stuff with $id or whatever?
}
Copy linkTweet thisAlerts:
@HarshShahOct 12.2020 — Hii Kiwis,

Try this one,
``<i>
</i>&lt;form method="post" action=""&gt;
&lt;span&gt;Select Technologies&lt;/span&gt;&lt;br/&gt;
&lt;input type="checkbox" name='lang[]' value="WPF"&gt; WPF &lt;br/&gt;
&lt;input type="checkbox" name='lang[]' value="Angular"&gt; Angular&lt;br/&gt;
&lt;input type="checkbox" name='lang[]' value="Xamarin"&gt; Xamarin &lt;br/&gt;
&lt;input type="checkbox" name='lang[]' value="MVC"&gt; MVC &lt;br/&gt;

&lt;input type="submit" value="Submit" name="submit"&gt;
&lt;/form&gt;

&lt;?php
if(isset($_POST['submit'])){

if(!empty($_POST['lang'])) {

foreach($_POST['lang'] as $value){
echo "value : ".$value.'&lt;br/&gt;';
}

}

}
?&gt;<i>
</i>
``

I hope it helps you!!!
Copy linkTweet thisAlerts:
@kiwisauthorOct 12.2020 — @NogDog#1624135

<input type="checkbox" name="foo{<?php echo $item->getId(); ?>}" value='{<?php echo $item->getTitle(); ?>}'>

Show's all my checkboxes okay, name['foo{22}'] etc

Using your foreach thought..

Warning: Invalid argument supplied for foreach() in
Copy linkTweet thisAlerts:
@NogDogOct 12.2020 — > @kiwis80#1624150 Warning: Invalid argument supplied for foreach() in

Did you use method="post" in the &lt;form&gt; tag? If that's not it, maybe do a print_r($_POST) to see where I screwed up? You could also take HarshShah's approach and just put the ID in as the value attribute, using empty brackets to get an enumerated array of submitted checkboxes.

> @kiwis80#1624150 Show's all my checkboxes okay, name['foo{22}'] etc

If that's what is actually output to the HTML, then something is wrong. It should look like name='foo[22]. I used the curly braces to interpolate an array value within a double-quoted string.
Copy linkTweet thisAlerts:
@kiwisauthorOct 12.2020 — What's the difference here? obviously it's picking up the post variable foo and the square braces allow other parameters to be passed in (who knew!) but the curly ones don't?? - why's that?
Copy linkTweet thisAlerts:
@NogDogOct 12.2020 — The curly braces do a couple things within a double-quoted string if they surround something starting with a $:

  • - They explicitly establish the boundaries of the variable name, useful if whatever immediate follows the variable name is not white-space or other non-word character that cannot be part of a variable name.

  • - Allow array variables within the string to have their indexes quoted. Otherwise you have to leave them unquoted in that situation (e.g. $foo[something] instead of the normal $foo['something']), but that will actually raise a notice-level error about that being a deprecated behavior (I think), but it's weird quoting them in some places and not in others.


  • But, if all that is ugly to you, you can always just use concatenation instead, or even sprintf() or such. These should all do the same thing, assuming no typos by me:
    <i>
    </i>echo "Blah blah {$foo['something']} blah blah" // or...
    echo "Blah blah " . $foo['something'] . " blah blah" // or...
    printf("Blah blah %s blah blah", $foo['something']);
    ×

    Success!

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