Get checkbox value in php

i have a php file that have some ckeckboxes in table, that are 'checked' or 'unchecked' by database value at the time of form load.
now i want 'check' or 'uncheck' the checkboxes and click a save link that update new 'checked' or 'unchecked' values in database.
 
i have save link for each database record in HTML table.
sudanshuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try:

1. Named your checkboxes with something like: chk[]
2. on your php scripts:

..

$query = "Delete from yourtable Where id = ".$id;
            $result = mysql_query($query); // or die("Query failed : " . mysql_error());
            
            //re-insert items checked
            foreach($_POST['chk'] as $chk) {
                  $query = "Insert into yourtable (id, chk_id) Values ($id,$chk) ";
                  $result = mysql_query($query) or die("Query failed : " . mysql_error());
            }

...


get the idea? try customize accordingly.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Note that unchecked checkboxes do not come through in your $_POST variable.  At all.

So if you have a checkbox in the data base that is checked, and you put up a form on the screen that shows it checked, and the client un-checks it, you will not have anything for that checkbox in $_POST.

Experiment with the code snippet and you'll see the effect.

HTH, ~Ray
<?php // RAY_checkboxes.php
if (!empty($_POST["_submit"])) {
   echo "<pre>"; var_dump($_POST); echo "</pre> \n";
}
?>
<html>
<body>
<form action="<?=$PHP_SELF?>" method="POST">
Check or Uncheck Some Boxes Here<br/>
<input name=box1 type="checkbox"><br/>
<input name=box2 type="checkbox"><br/>
<input name=box3 type="checkbox"><br/>
<input name=box4 type="checkbox"><br/>
<br />
<input type="submit" name="_submit"  value="go" />
</form>
</body>
</html>

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.