检查具有来自php中数据库的值的复选框

时间:2013-09-06 02:06:16

标签: php

当我在数据库中提交我选中的复选框时,我想保留选中的复选框..请帮我在这里做什么...这只是我的片段..

<?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>

3 个答案:

答案 0 :(得分:1)

<?
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>

答案 1 :(得分:0)

您可以指定复选框是否为checked by default in HTML

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

VS

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

您可以使用PHP动态修改HTML,以便在加载页面时选中或取消选中该框:

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

用于跟踪复选框状态的标志可以是(1)由PHP中的SQL查询,(2)以PHP session传递,或(3)作为POST数据等确定。 / p>

答案 2 :(得分:0)

以下是关于如何使用存储的值数组实现此功能的一些想法;它还使用PDO和准备好的陈述。

如果有任何不清楚的地方,请查看并告诉我。

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));