/    Sign up×
Community /Pin to ProfileBookmark

Array of Textboxes

In this sample I am using PHP to create 8 text boxes, the user types in info then submits form, but when I pull the data entered using the code below I am only getting the data typed into the last text box. The below example may not have a logical use, but I just simplified the script to try to understand why it is not working.

[code=php]<?php
$box = array(1, 2, 3, 4, 5, 6, 7, 8);
foreach($box as $boxes) {
echo'<li class=”box”><input name=”mytable[$boxes]” type=”text “size=”12″/></li>’;
}
?>[/code]

Display Page

[code=php]<?php
$textboxes = $_POST[“mytable”];
foreach($textboxes as $txt)
{
echo $txt;
}
?>[/code]

Thanks

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 13.2015 — At a wild stab in the dark, maybe the HTML form isn't quite kosher, but no way to tell from the code you provided. Never hurts to just dump out $_POST and see what you actually got, so you know which side the problem is on:

[code=php]
die("<pre>POST:n".print_r($_POST, 1)."</pre>");
[/code]
Copy linkTweet thisAlerts:
@lexiferrauthorMay 13.2015 — When I do the dump I get only the last text box data, this is what the dump shows after loading 8 text boxes and typing A through H in the boxes.
[code=php]POST:
Array
(
[mytable] => Array
(
[$boxes] => h
)

)[/code]


The full code including form:
[code=html]
<form action="print.php" name="genForm" method="post">
<main>

<ul><?php
$box = array(1, 2, 3, 4, 5, 6, 7, 8);
foreach($box as $boxes) {
echo'<li class="box"><input name="mytable[$boxes]" type="text "size="12"/></li>';
}
?></ul>


</main>

<input type="submit" value="Print" "button" style="height: 40px; width: 180px; font-size: 17px;" />


</form>[/code]


And the print page
[code=php]<main>

<?php
die("<pre>POST:n".print_r($_POST, 1)."</pre>");

$textboxes = $_POST["mytable"];
foreach($textboxes as $txt)
{
echo $txt;
}
?>

</main>[/code]


Thanks
Copy linkTweet thisAlerts:
@ginerjmMay 13.2015 — Reverse the usage of quotes on this line:

foreach($box as $boxes) {

echo'<li class="box"><input name="mytable[$boxes]" type="text "size="12"/></li>';


Your php var is not being interpreted because it is wrapped in a single quote primarily.

foreach($box as $boxes) {

echo "<li class='box'><input name='mytable[$boxes]' type='text' size='12'/></li>";

Of course you could simply eliminate $boxes and let the array indices be assigned, starting with 0.
×

Success!

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