•
•
•
•
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
![]() |
•
•
Join Date: Jun 2008
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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.
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.
•
•
Join Date: Jun 2008
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
Your checkboxes need to be an array that php can loop through. All should have aname="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."
•
•
Join Date: Jun 2008
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
Your checkboxes need to be an array that php can loop through. All should have aname="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.
•
•
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 554
Reputation:
Rep Power: 3
Solved Threads: 57
•
•
Join Date: Jun 2008
Posts: 47
Reputation:
Rep Power: 1
Solved Threads: 2
[offtopic]
I see you use several shashes in one line of code:
Why not do it this way?
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:
And no injection can be applied.
I see you use several shashes in one line of code:
php Syntax (Toggle Plain Text)
echo "<td width=\"75\" align=\"center\"><input type=\"checkbox\" name=\"toApprove[]\" id=\"approve\" value=\"approve\" />";
Why not do it this way?
php Syntax (Toggle Plain Text)
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:
php Syntax (Toggle Plain Text)
foreach($_POST['chkField'] as $value){ $sql="DELETE FROM TABLE WHERE id='".mysql_real_escape_string($value)."'"; $result=mysql_query($sql); }foreach($_POST['chkField'] as $value){ $sql="DELETE FROM TABLE WHERE id='".mysql_real_escape_string($value)."'"; $result=mysql_query($sql); }
And no injection can be applied.
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the PHP Forum
- Previous Thread: Displaying images retrieved from a mysql db using the image path
- Next Thread: Mail function


Linear Mode