Krishna
Krishna

Reputation: 157

check the checkbox that has values coming from database in php

when i submit my checked checkbox in the database i want to retain the checked check box.. please help me what to do with here.. this is only my snippet..

<?php
if (isset($_POST['submit'])) {
    $radha        = $_POST['radha'];
    $radha_values = '';
    foreach ($radha as $val) {
        $radha_values .= $val . ",";
    }
    $sql = "INSERT INTO tbl_check (names) VALUES ('$radha_values')";
    $res = mysql_query($sql) or die("error:" . mysql_error());
    if ($res) {
        echo "added";
    }
    mysql_close();
}
$sql = "select names from tbl_check where id=2";
$res = mysql_query($sql) or die("error:" . mysql_error());
$row = mysql_fetch_array($res);
$names = $row['names'];
echo $names;
?>
<form method="post" action="checkbox.php">
    <input name="radha[]" type="checkbox" value="krishna"/> krishna
    <input name="radha[]" type="checkbox" value="gopala"/>gopala
    <input name="radha[]" type="checkbox" value="govinda"/>govinda
    <input name="radha[]" type="checkbox" value="haribol"/>haribol<br/>
    <input name="submit" type="submit" value="add"/>
</form>

Upvotes: 0

Views: 1739

Answers (3)

Liam Allan
Liam Allan

Reputation: 1115

try

<?
if (isset($_POST['submit'])) {
    $radha        = $_POST['radha'];
    $radha_values = '';
    foreach ($radha as $val) {
        $radha_values .= $val . ",";
    }
    $sql = "INSERT INTO tbl_check (names) VALUES ('$radha_values')";
    $res = mysql_query($sql) or die("error:" . mysql_error());
    if ($res)
        echo "added";
    mysql_close();
}

$sql = "select names from tbl_check where id=2";
$res = mysql_query($sql) or die("error:" . mysql_error());
$row   = mysql_fetch_array($res);
$names = $row['names'];
$name  = explode(',', $names);
foreach ($name as $checked) {

    if (strpos($checked, 'krishna') !== false) {
        $krishna = 'checked';
    }
    if (strpos($checked, 'gopala') !== false) {
        $gopala = 'checked';
    }
    if (strpos($checked, 'govinda') !== false) {
        $govinda = 'checked';
    }
    if (strpos($checked, 'haribol') !== false) {
        $haribol = 'checked';
    }

}
?>

<form method="post" action="checkbox.php">
    <input name="radha[]" type="checkbox" value="krishna" <?php echo $krishna;?>/> krishna
    <input name="radha[]" type="checkbox" value="gopala"  <?php echo $gopalaa;?>/>gopala
    <input name="radha[]" type="checkbox" value="govinda" <?php echo $govinda;?>/>govinda
    <input name="radha[]" type="checkbox" value="haribol"  <?php echo $haribol;?>/>haribol<br />
    <input name="submit" type="submit" value="add"  />
</form>

Upvotes: 1

Ja͢ck
Ja͢ck

Reputation: 173562

Here's some idea of how you could implement this using a stored array of values; it also uses PDO and prepared statements.

Have a look and let me know if anything is unclear.

function showRadhaCheckboxes(array $values, array $checked = array())
{
    $checked = array_flip($checked);

    foreach ($values as $value => $title) {
        echo sprintf('<input name="radha[%s]" type="checkbox" value="1"%s /><span>%s</span>',
            htmlspecialchars($value, ENT_QUOTES, 'UTF-8'),
            array_key_exists($value, $checked) ? ' checked="checked"' : '',
            htmlspecialchars($title, ENT_QUOTES, 'UTF-8')
        );
    }
}

storeRadhaValues(PDO $db, array $post)
{
    $stmt = $db->prepare('INSERT INTO tbl_check (names) VALUES (:values)');
    $stmt->execute(array(
        ':values' => join(',', array_keys($post))
    ));
}

loadRadhaValues(PDO $db, $id)
{
    $stmt = $db->prepare('SELECT names FROM tbl_check WHERE id = :id');
    $stmt->execute(array(
        ':id' => $id
    ));

    $names =current($stmt->fetchAll(PDO::FETCH_COLUMN));

    return explode(',', $names);
}

$db new PDO(...);

showRadhaCheckboxes(array(
        'krishna' => 'krishna',
        'gopala' => 'gopala',
        'govinda' => 'govinda',
        'haribol' => 'haribol',
), loadRadhaValues($db, 2));

Upvotes: 0

xikkub
xikkub

Reputation: 1660

You can specify whether a checkbox is checked by default in HTML:

<input type="checkbox" name="vehicle" value="Car" checked>

vs

<input type="checkbox" name="vehicle" value="Car">

You can dynamically modify the HTML using PHP to check or uncheck the box when the page is loaded:

echo '<input type="checkbox" name="vehicle" value="Car"';
if ($checkedStatus == True) echo ' checked';
echo '>'

The flag you use to keep track of the checkbox status can be (1) determined by a SQL query in PHP, (2) passed with a PHP session, or (3) as POST data, etc.

Upvotes: 0

Related Questions