0

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>
3
  • It would be easier if the checkbox values were in an array. Commented Sep 6, 2013 at 2:39
  • yes.. i wast actually thinking about it.. but i dont exactly know how.. can you please give me some snippet? Commented Sep 6, 2013 at 2:57
  • Btw, your code has XSS and SQLi vulnerabilities. Commented Sep 6, 2013 at 3:51

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

1 Comment

yes thank you for this answer but i guess i should first study PDO.. and that will took me a long time.. because i havent actually used it before thanks @Jack

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.