PHP Dynamic Checkboxes
By Jamsi • Feb 7th, 2006 • Category: PHPI noticed the lack of tutorials or articles when using checkboxes in a dynamic form, so I thought I’d post some code I used recently to manage deleting rows. The thing most people forget isthat the checkbox field name has to have a [] on the end as this tells PHP to use an array and not a normal variable to hold the information.
PHP
1 2 3 4 5 6 | if ($_REQUEST["btnDelete"]) { foreach ($_REQUEST["chkDelete"] as $value) { echo $value; } } |
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <form action="yourfile.php" method="post" name="form1"> <table width="70%" border="0" cellpadding="2" cellspacing="2"> <tr> <td width=5%></td>d> <td>Username</td> </tr> <?php $result = mysql_query("select * from TABLE"); while ($row = mysql_fetch_array($result)) { echo "<tr>"; echo " <td><input type=checkbox value=".$row[userid]." name=chkDelete[]></td>"; echo " <td>".$row[username]."</td>"; echo "</tr>"; } ?> </table> <input type="submit" value="Delete Selected" name="btnDelete"> </form> |
No related posts.
FREE NEWSLETTER -> Want Tech Tips Sent Straight to your Inbox?
Grab our Newsletter to Ensure your PC is Running Smooth!
Jamsi is currently studying a full time Bachelor of Computer/Business degree and working part time as an Internet Security Consultant.
Email this author | All posts by Jamsi
























Thanks.
It worked like this for me
if ($_REQUEST["chkDelete"])
{
foreach ($_REQUEST["chkDelete"] as $value)
{
echo $value;
}
}
Whoops, thanks! Forgot a bracket
Help me please!
What table structure i must have?
I have:
`userid` varchar(255) NOT NULL,
`username` varchar(255) NOT NULL,
`visable` tinyint(1) NOT NULL default ‘1′,
and trying (my lines 28,29):
echo ” “;
echo ” “.$row[username].”";
and having next error”
Notice: Use of undefined constant visable - assumed ‘userid’ in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test-check-add.php on line 28
Notice: Use of undefined constant username - assumed ‘username’ in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test-check-add.php on line 29
I whant to change visable value from 1 to 0 if checkbox is not checked. Plz, help, i am newbie…
Best places to play, best strategies to win. Player ratings & reviews of top online casinos & pokers with highest payouts & biggest bonuses.