Jump to content

Archived

This topic is now archived and is closed to further replies.

DarkHavn

$_POST Question, silly i know

Recommended Posts

[code]

echo '' . $user['name'] . '<input type="checkbox" name=' . $user["name"]. '><br/>';

[/code]

Ok that's my following check box, it echo's one out for every user that is registered.

This form posts to a process.php page, now i'm wondering i've tryed foreach(), while(), but how can i pick this up in the
$_POST['']; ?

As the name=' . $user["name"]. ' value is whatever comes from the database

Thanks again for the help guys/girls :)

Share this post


Link to post
Share on other sites
If I'm not mistaken you have to give it a value or it won't post anything. The name of the input type is the variable that it will POST as, and the value is what that variable is equal to.

You need to have both a name and a value for your checkbox.

You will need to add the value=' . $user['name'] . ' to your input tag. Also how many of these do you want to be able to select? The way you have it now you will be able to select every single user because the name of the input is different for each one.

Share this post


Link to post
Share on other sites
So i would need to loop through the post to get the proper value.

Hrmm, well this is just a tester at the moment, but once the script does go live, it will handle alot of accounts.

This section is used for deleting users, i've looped through the value's and gotten the name that im after, but i can't seem to split the $_POST array to get the value that i want.

I'm abit confused as well on this

Share this post


Link to post
Share on other sites
[code]foreach($_POST as $k=>$v){
    $sql="DELETE FROM table WHERE userid='$v'";
    mysql_query($sql);
}[/code]

That will simply grab every post variable and attempt to delete any row from the tabel that has a userid set to the value of the post.

Share this post


Link to post
Share on other sites
Ok that wasn't working, so i echoed out the variable, and what i got was


Arrayon
Delete

that was echoing out the $v (value) variable, so i echoed out the $k (key) variable, and got that i was looking for, but it has nearly every post value in it as well (including the ones form the buttons)

Hrmm so this has confused me even more lol

Share this post


Link to post
Share on other sites
That will happen. Prepend something like 'deleteme:' before the username when declaring the Input so that when you loop through the keys you can only bother with ones that start with that.

In otherwords you'll get values like this in $_POST[]:
deleteme:sam => on
deleteme:bill => on
Delete

The check boxes which are checked should appear in the array and have the value 'on', and those not checked should not appear at all.

You might want to build a string of names like this ('sam', 'bill', 'bob') and use the SQL contruct 'In' to remove all of them at once. I hope your names are a unique key in your table, otherwise you should obviously be using one for the deletions. (I hope that's obvious.)

Sam.

Share this post


Link to post
Share on other sites
Hrmm wouldn't i need to cut the string apart just to get the username it's self without the delete: me segment?

Otherwise sql wouldn't recognise the data to delete.

Hrmm don't think the name is set up as a unique key, it's in a table with other feilds like last name, email etc, the main being an id that's the primary with an auto_increment extra.

Share this post


Link to post
Share on other sites
Yes you would. Hardly a bother though as you can write the statement like this:

if (substr($key, 0, 9) == 'deleteme:') $deletelist .= ", ".substr($key, 9);

Or something like that.

I haven't put quotes around the value in the list as the more serious problem you're going to have is not worrying about a little inelegance in splicing a few string, but rather in accidentally deleting people with the same names. Rather than using names, make sure the table has a numeric primary key and use that. Might be able to get away with is_numeric($key) rather than the string splicing then too.





Share this post


Link to post
Share on other sites
Hrmm ok i would do that, but the only problem being that the $key seems to hold some more information, like the submit button name/value, and an array

The following code is how ive output it see below

[code]
if($_POST['agdel']) {

array_multisort($_POST);

foreach($_POST as $key => $value) {

echo "$value<br/>";



}


}
[/code]

The following output i get is


ArrayDelete
random


the output i want is 'random' how would i access this from the $value variable? as there seems to be the one before that

'arraydelete'

???

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.