Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting checkboxes' value from within PHP script 1

Status
Not open for further replies.

DaSe

Programmer
Feb 14, 2008
149
GB
Hello guys and Phil...
Just would like to make sure if I'm doing the thing right and if so - what I am doing wrong then... ( ok not funny..)

The problem is: I have an array of added members in a table and I added a checkbox to each member so that the user could delete the member when the checkbox is checked next to it. So..the code within the while PHP loop is:
[/code]

$mem_dir_add_pic .= "<a href='see_member.php?id=$user_id'><img src='members/avatar/avatar.jpg' width='41' height='60' border='1' align='left' hspace='3' vspace='5' style='border-color:black' alt='' title='' style='float:left'/><span class='style2' style='float:left;margin-top:67px;margin-left:-46px'>&nbsp;".$user."&nbsp;<input type='checkbox' name='del_mem' value='".$user_id."' /></span></a>";

[/code]

But when below I add the code:
Code:
//remove members
$sel_rem = mysql_query("SELECT * FROM `members`");

$mem_check = $_POST['del_mem'];
print $mem_check;
if ($mem_check == $user_id) { 

$proceed = mysql_query("DELETE FROM `members` WHERE friend_id='$user_id' AND user_id='$uid' ");
 
}
Code:
HTML code:

<form 
        name="remove" action="remove_members.php" method="post"><input type="submit"  name="submit" value="go" /></form>


The values from the checkboxes are not received...

Regards for any comments..


 
you might want to put the checkboxes inside the form. forms only submit controls that are within their tags.
bear in mind too that even if they were inside the form only the last value would be passed as you have named them all the same.
perhaps this would be better

Code:
<input type='checkbox' name='del_mem[$user_id]' value='on' />
 
So you're suggesting putting the <input type> within <form> tags in HTML ? I think the checkboxes need to stay within PHP script in a while loop. I was trying to envelope them with <form> tags in PHP :

Code:
$mem_dir_add_pic .= "<a href='see_member.php?id=$user_id'><img src='members/avatar/avatar.jpg' width='41' height='60' border='1'  align='left' hspace='3' vspace='5' style='border-color:black' alt='' title='' style='float:left'/><span class='style2' style='float:left;margin-top:67px;margin-left:-46px'>&nbsp;".$user."&nbsp;<form method='post'><input type='checkbox' name='del_mem' value='".$user_id."' /></span></a></form>";

 
It looks like input type is working fine within PHP - I just don't get why it doesn't return value/name through POST or GET. Even if enveloped in <form> tags. Maybe it has to do anything with submit button placed in html ? Regards..

 
The solution that works for me is to place 'checkboxes' within PHP as I did and put them also inside <form> tags whereas 'submit' button is an img with onclick javascript that fetches 'getDocumentById' from the form of checkboxes in PHP.

 
Jpadie, now I know what you meant by saying "bear in mind too that even if they were inside the form only the last value would be passed as you have named them all the same.
". I've managed to fetch the value from checkboxes but only the last value seems to be returned. The thing is this: Each checkbox of each picture has its own correct value - which is $user_id. But when I try to check the picture to delete it - only the last picture gets deleted. I was playing with this solution but to no avail now.

Code:
$mem_dir_add_pic .= "<form method='post' name='myform' id='myform' action='remove_members.php'><a href='see_member.php?id=$user_id'><img src='members/avatar/avatar.jpg' width='41' height='60' border='1'  align='left' hspace='3' vspace='5' style='border-color:black' alt='' title='' style='float:left'/><span class='style2' style='float:left;margin-top:55px;margin-left:-46px'>&nbsp;".$user."&nbsp;</span></a><span style='float:left;margin-top:2px;margin-left:-5px'><input type='checkbox' name='del_mem[]' id='".$user_id."' value='on' /></span></form>";


$mem_check = $_POST['del_mem'];


while (list($key,$val) = @each ($mem_check)) { 
echo "$val,";

} 

// the $val returns only last value..

if (($mem_check == true) || ($mem_check2 == true))  { 

$proceed = mysql_query("DELETE FROM `members` WHERE friend_id='$mem_check' OR friend_id='$mem_check2' AND user_id='$uid' ");


}

Thanks muchly for any comments..

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top