/    Sign up×
Community /Pin to ProfileBookmark

Displaying Arrays

I have an HTML form that has some checkboxes like this:

[code=html]
<tr>
<td align=”left” class=”choice”>Choice One <input type=”checkbox” name=”description[]” value=”choice_one” /></td>
<td align=”left” class=”choice”>Choice Two& <input type=”checkbox” name=”description[]” value=”choice_two” /></td>
<td align=”left” class=”choice”>Choice Three <input type=”checkbox” name=”description[]” value=”choice_three” /></td>
</tr>
[/code]

And then I want to print it our for a form with PHP like this:

[code=php]
if(is_array($_POST[‘description’])) {
foreach ($_POST[‘description’] as $value) {
$description .= $value . “<br />”;
}
}
[/code]

But when I echo $description, it always precedes the values with the word “Array”…how do I avoid that?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@bsmbahamasSep 16.2011 — [CODE]

if(is_array($_POST['description'])) {
$entries = sizeof( $_POST['description'] );
for ( $i = 0; $i < $entries; $i++ ) {
$description .= $_POST['description'][$i] . "<br />";
}

}
echo $description;
[/CODE]


or maybe...

[CODE]

if(is_array($_POST['description'])) {
$data = $_POST['description'];
$entries = sizeof($data);
for ( $i = 0; $i < $entries; $i++ ) {
$description .= $data[$i] . "<br />";
}

}
echo $description;
[/CODE]
Copy linkTweet thisAlerts:
@BelrickSep 16.2011 — Nevermind
Copy linkTweet thisAlerts:
@NogDogSep 17.2011 — I'm guessing you have the deprecated [url=http://php.net/manual/en/security.globals.php]register_globals[/url] option enabled, so you already have a $description array set from the form submission.

The best fix would be to have that option turned off in your PHP configuration; but if that is not feasible (due to other code depending on it, or not having access to the configuration file), then you could initialize $description to null or an empty string before using it in your loop.
[code=php]
$description = '';
if(is_array($_POST['description'])) {
foreach ($_POST['description'] as $value) {
$description .= $value . "<br />";
}
}
[/code]
Copy linkTweet thisAlerts:
@eval_BadCode_Sep 19.2011 — [code=php]

$str = '<td align="left" class="choice">Choice &#37;d <input type="checkbox" name="description[]" value="choice_%d" />%s</td>';

foreach($description as $k => $choice) printf($str,$k,$k,htmlentities($choice));

//other direction

foreach($_POST['description'] as $i => $v) printf("%d ~~ %s<br />",$i,htmlentities($v));

[/code]
×

Success!

Help @Ntrimgs 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.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...