?
Solved

Get checkbox value in php

Posted on 2008-11-06
2
Medium Priority
?
6,144 Views
Last Modified: 2013-12-13
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.
0
Comment
Question by:sudanshu
2 Comments
 
LVL 56

Accepted Solution

by:
Ryan Chong earned 750 total points
ID: 22902191
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
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 750 total points
ID: 22904391
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question