/    Sign up×
Community /Pin to ProfileBookmark

Help with POSTing array.

The code below is intended to allow a site administrator to manage users/passwords.

The output is formatted into a table where:
1st row is the [b]Update Records[/b] button used to submit the form for processing,
2nd row is the headers for each column,
3rd row allows Administrator to add a new user/password,
4th+ row lists existing users and their passwords. (Password is put into a textbox to allow changes.) (A checkbox in the first columns is intended to allow the administrator to flag users for deletion.)

The data is sent to [b]updateuser.php[/b] for processing using the POST method. However I don’t seem to be getting all the data at the other end.

What I am getting is:
If a new UserID and Password are entered, they are received.
I am receiving the Password of the last user in the list.
If one of the checkboxes is on, I am getting ON, but don’t know which row it refers to.

What I am not getting is:
I am not getting the full array of all the checkboxes,
I am not getting the full array of passwords.

[SIZE=3][b]I assume that in my code, I am not creating an array of Checkbox and Text contols. Can someone please advise how to fix this.[/b][/SIZE]

Cheers,
Nap

Note that:
$namePassCombos[$i][0] contains the user’s ID
$namePassCombos[$i][1] contains the user’s password.

[code]
echo “<form method=’POST’ action=’updateusers.php’ >”;
echo “<table width=’450′ border=’1′ cellspacing=’0′ cellpadding=’6′>”;
echo “<tr>”;
echo “<td colspan=3><input type=’submit’ name=’submit’ value=’Update Records’></td>”;
echo “</tr>”;
echo “<tr>”;
echo “<td width=’20%’ align=’center’ bgcolor=’coral’ >Delete</td>”;
echo “<td width= ‘40%’ align=’Left’ bgcolor=’coral’ >User ID</td>”;
echo “<td width= ‘40%’ align=’Left’ bgcolor=’coral’ >Password</td>”;
echo “</tr>”;
echo “<tr>”;
echo “<td align=’center’ bgcolor=’aqua’ >New User</td>”;
echo “<td align=’Left’ bgcolor=’aqua’ ><input type=’text’ name=’userid’></td>”;
echo “<td align=’Left’ bgcolor=’aqua’ ><input type=’text’ name=’userpassword’></td>”;
echo “</tr>”;

for ($i=0; $i <= $index; $i++)
{
echo “<tr>”;
echo “<td align=’center’ bgcolor=’beige’ ><input type=’checkbox’ name=’deletemember’ /></td>”;
echo “<td align=’Left’ bgcolor=’beige’ >”.$namePassCombos[$i][0].”</td>”;
echo “<td align=’Left’ bgcolor=’beige’ ><input type=’text’ name=’password’ value=”.$namePassCombos[$i][1].”></td>”;
echo “</tr>”;
}

echo “</table>”;
[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 08.2006 — Checkboxes are only received in the Post data if they are checked. Also, if you create multiple input elements with the same name, the last one sent will overwrite all the others with the same name, unless you append a "[]" to then end of the name, in which case it will be added to an array by that name. If you add a unique value to each checkbox, then you can figure out which ones were submitted. So you might do something like:
[code=php]
for ($i=0; $i <= $index; $i++)
{
echo "<tr>";
echo "<td align='center' bgcolor='beige' >".
<input type='checkbox' name='deletemember[]' value=".$namePassCombos[$i][0]."/></td>";
echo "<td align='Left' bgcolor='beige' >".$namePassCombos[$i][0]."</td>";
echo "<td align='Left' bgcolor='beige' ><input type='text' name='password[]' value=".$namePassCombos[$i][1]."></td>";
echo "</tr>";
}
[/code]

That should give you two arrays: $_POST['deletemember'] and $_POST['password']. You could then loop through each $_POST['deletemember'] and pair it up with the matching value in $_POST['password'].
Copy linkTweet thisAlerts:
@NapauthorMar 08.2006 — NogDog,

You're a lifesaver. I have done what you suggested, and it works great expect for the checkbox.

What you said about checkboxes only being sent if they are on is a problem for me. I was hoping to use the Element Numbers to detemine which Users are to be deleted. Is there another way?

Is it possible to use string concatination?

<i>
</i>(inside the loop with $i as index)

$delmem = "deletemember" + $i
echo "&lt;td align='center' bgcolor='beige' &gt;&lt;input type='checkbox' name='$delmem' /&gt;&lt;/td&gt;";



Regards,

Nap
Copy linkTweet thisAlerts:
@NapauthorMar 08.2006 — Fixed it myself.

Now I have an list of variables that start with the same name but have a number at the end designating the index position in the 'array'.

Each checkbox ticked comes though with a different variable name but it will be easy to line it up with the other array.

<i>
</i>for ($i=0; $i &lt;= $index; $i++)
{
[b]$delmem = "deletemember".$i;[/b]
echo "&lt;tr&gt;";
echo "&lt;td align='center' bgcolor='beige' &gt;&lt;input type='checkbox' name="[b].$delmem.[/b]" /&gt;&lt;/td&gt;";
echo "&lt;td align='Left' bgcolor='beige' &gt;".$namePassCombos[$i][0]."&lt;/td&gt;";
echo "&lt;td align='Left' bgcolor='beige' &gt;&lt;input type='text' name='password[]' value=".$namePassCombos[$i][1]."&gt;&lt;/td&gt;";
echo "&lt;/tr&gt;";
}



Cheers,

Nap
×

Success!

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