User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 425,779 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,312 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 714 | Replies: 5
Reply
Join Date: Jun 2008
Posts: 3
Reputation: paldss is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
paldss paldss is offline Offline
Newbie Poster

determining if a checkbox is checked

  #1  
Jun 6th, 2008
Hello all. I have an HTML page that displays a list of comments left on a site. The administrator needs to go in and put checks next to the comments he wants to delete, and click submit.

echo "<td width=\"75\" align=\"center\"><input type=\"checkbox\" name=\"toApprove[]\" id=\"approve\" value=\"approve\" />";

echo "<td width=\"75\" align=\"center\"><input type=\"checkbox\" name=\"toDelete[]\" id=\"delete\" value=\"delete\" />";

echo "<input type=\"submit\" name=\"approve\" value=\"Approve\" /></td>";

echo "<td><input type=\"submit\" name=\"delete\" value=\"Delete\" /></td></form></tr>";


The submit goes to a php page with the code that should do the deleting. But that's where I am stuck.

I cannot seem to get the data on the state of the checkbox to go to the php page.

It should work out that the admin can, for example, put a check on messages 1 and 3, hit submit, and they are deleted for the file. Leaving only messages 1 and 4.

I really appreciate and help I can get. Thanks in advance.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: Arkansas
Posts: 396
Reputation: buddylee17 will become famous soon enough buddylee17 will become famous soon enough 
Rep Power: 2
Solved Threads: 78
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Whiz

Re: determining if a checkbox is checked

  #2  
Jun 6th, 2008
Your checkboxes need to be an array that php can loop through. All should have a name="chkField[]" or something similar. The value is the only thing that should be different between one checkbox and the next. Then in the form processor,
foreach($_POST['chkField'] as $value){
$sql="DELETE FROM TABLE WHERE id=$value";
$result=mysql_query($sql);
}
Last edited by buddylee17 : Jun 6th, 2008 at 10:36 pm.
Reply With Quote  
Join Date: Jun 2008
Posts: 3
Reputation: paldss is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
paldss paldss is offline Offline
Newbie Poster

Re: determining if a checkbox is checked

  #3  
Jun 8th, 2008
Originally Posted by buddylee17 View Post
Your checkboxes need to be an array that php can loop through. All should have a name="chkField[]" or something similar. The value is the only thing that should be different between one checkbox and the next. Then in the form processor,
foreach($_POST['chkField'] as $value){
$sql="DELETE FROM TABLE WHERE id=$value";
$result=mysql_query($sql);
}



Thanks for the info. When I try to run this the only output I get is 'Array'. I try to echo the $chkfield and $value just to test what the output is, and it always comes back "Array."
Reply With Quote  
Join Date: Jun 2008
Posts: 3
Reputation: paldss is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
paldss paldss is offline Offline
Newbie Poster

Re: determining if a checkbox is checked

  #4  
Jun 8th, 2008
Originally Posted by buddylee17 View Post
Your checkboxes need to be an array that php can loop through. All should have a name="chkField[]" or something similar. The value is the only thing that should be different between one checkbox and the next. Then in the form processor,
foreach($_POST['chkField'] as $value){
$sql="DELETE FROM TABLE WHERE id=$value";
$result=mysql_query($sql);
}



Thanks for the info. When I try to run this the only output I get is 'Array'. I try to echo the $chkfield and $value just to test what the output is, and it always comes back "Array."

I'm sure I am making this harder than it is. I guess I have just stared at it too long, and it all looks the same now.
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 554
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 57
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Posting Pro

Re: determining if a checkbox is checked

  #5  
Jun 8th, 2008
if you are trying to echo an array you should use print_r($array).
Reply With Quote  
Join Date: Jun 2008
Posts: 47
Reputation: kvdd is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kvdd kvdd is offline Offline
Light Poster

Re: determining if a checkbox is checked

  #6  
Jun 9th, 2008
[offtopic]
I see you use several shashes in one line of code:
  1. echo "<td width=\"75\" align=\"center\"><input type=\"checkbox\" name=\"toApprove[]\" id=\"approve\" value=\"approve\" />";

Why not do it this way?
  1. echo '<td width="75" align="center"><input type="checkbox" name="toApprove[]" id="approve" value="approve" />';

Note the difference: the ' and the "
[/offtopic]

Ontopic:
If your checkbox is not checked, it does not POST the value..

By the way the code posted above is not injection proof, use this code instead:
  1. foreach($_POST['chkField'] as $value){
  2. $sql="DELETE FROM TABLE WHERE id='".mysql_real_escape_string($value)."'";
  3. $result=mysql_query($sql);
  4. }foreach($_POST['chkField'] as $value){
  5. $sql="DELETE FROM TABLE WHERE id='".mysql_real_escape_string($value)."'";
  6. $result=mysql_query($sql);
  7. }

And no injection can be applied.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the PHP Forum

All times are GMT -4. The time now is 2:53 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC