Jump to content


Photo

$_POST Question, silly i know


  • Please log in to reply
9 replies to this topic

#1 DarkHavn

DarkHavn
  • Members
  • PipPipPip
  • Advanced Member
  • 69 posts

Posted 23 August 2006 - 12:26 AM


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


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 :)

#2 jcbarr

jcbarr
  • Members
  • PipPipPip
  • Advanced Member
  • 219 posts

Posted 23 August 2006 - 12:36 AM

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.

#3 DarkHavn

DarkHavn
  • Members
  • PipPipPip
  • Advanced Member
  • 69 posts

Posted 23 August 2006 - 12:46 AM

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

#4 jcbarr

jcbarr
  • Members
  • PipPipPip
  • Advanced Member
  • 219 posts

Posted 23 August 2006 - 12:56 AM

foreach($_POST as $k=>$v){
    $sql="DELETE FROM table WHERE userid='$v'";
    mysql_query($sql);
}

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.


#5 DarkHavn

DarkHavn
  • Members
  • PipPipPip
  • Advanced Member
  • 69 posts

Posted 23 August 2006 - 10:46 AM

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

#6 SammyP

SammyP
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts
  • LocationLondon

Posted 23 August 2006 - 11:14 AM

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.
If this has solved your problem, a quick 'Thanks' is appreciated, but marking the thread as 'Solved' is better - it saves a lot of time.

Sam.

#7 DarkHavn

DarkHavn
  • Members
  • PipPipPip
  • Advanced Member
  • 69 posts

Posted 23 August 2006 - 11:53 AM

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.

#8 DarkHavn

DarkHavn
  • Members
  • PipPipPip
  • Advanced Member
  • 69 posts

Posted 23 August 2006 - 12:23 PM

?

#9 SammyP

SammyP
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts
  • LocationLondon

Posted 23 August 2006 - 12:40 PM

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.






If this has solved your problem, a quick 'Thanks' is appreciated, but marking the thread as 'Solved' is better - it saves a lot of time.

Sam.

#10 DarkHavn

DarkHavn
  • Members
  • PipPipPip
  • Advanced Member
  • 69 posts

Posted 24 August 2006 - 02:24 AM

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

if($_POST['agdel']) {

array_multisort($_POST);

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

echo "$value<br/>";



} 


}

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'

???




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users