i already searched about displaying the data fields, my problem is how to retain the checked item and then set its value as 1. please help. i tried some code but didn't work:

<html>
<form action='' method='post'>
<?php
$database = 'sample';
$table = 'checklist_stud_columns';

$mysql = mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('sample', $mysql) or die(mysql_error($mysql)); // selecting db is not not necessary in this case


$query = sprintf("
    SELECT
        COLUMN_NAME
    FROM
        INFORMATION_SCHEMA.COLUMNS
    WHERE
        TABLE_SCHEMA = '%s'
        AND TABLE_NAME = '%s'
",
    mysql_real_escape_string($database),
    mysql_real_escape_string($table)
);
$result = mysql_query($query, $mysql) or die(mysql_error($mysql));


while( false!=($row=mysql_fetch_array($result)) ) {
    $name = htmlspecialchars($row['COLUMN_NAME']);
    printf("<input type=\"checkbox\" name=\"col[]\" value=\"%s\" />%s (%s)<br />\r\n", $name, $name, $type);
}
if(isset($_POST['submit'])) {
    $checked = ($name==1) ? 'checked="checked"' : '';
    $checked = ($type==1) ? 'checked="checked"' : '';
    mysql_query("UPDATE checklist_stud_columns SET $name=1") or trigger_error(mysql_error(),E_USER_ERROR);
    printf("<input type=\"checkbox\" name=\"col[]\" value=\"%s\" />%s (%s)<br />\r\n", $name, $name, $type);
    }
?>
<tr><td colspan="2"><input type="submit" name="submit" value="Update Privileges" /></td></tr>
</form>
</html>
Member Avatar for diafol

Not sure what you're trying to do with this. A simple way...

If sending form data to the same page:

<?php
    $somefield = (isset($_POST['somefield'])) ? ' checked' : '';
    $somefield2 = (isset($_POST['somefield2'])) ? ' checked' : '';
?>

<form method="post">
    <input type="checkbox" name="somefield"<?php echo $somefield;?> />
    <input type="checkbox" name="somefield2"<?php echo $somefield2;?> />
    <input value="Send" type="submit" />
</form>

If sending to different page for processing (and then returning)...
Could be done with sessions...

processing.php
<?php
    session_start();
    //do processing... then if all OK
    $_SESSION['formpost'] = $_POST;
    header("Location:form.php");
?>
form.php
<?php
    session_start();
    if(isset($_SESSION['formpost']))
    {
        $post = $_SESSION['formpost'];
        unset($_SESSION['formpost']);
    }
    $somefield = (isset($post['somefield'])) ? ' checked' : '';
    $somefield2 = (isset($post['somefield2'])) ? ' checked' : '';
?>
<form method="post" action="processing.php">
    <input type="checkbox" name="somefield"<?php echo $somefield;?> />
    <input type="checkbox" name="somefield2"<?php echo $somefield2;?> />
    <input value="Send" type="submit" />
</form>

The second method is obviously more complicated but it does give the benefit of not resubmitting the form if you refresh/reload the page. BTW - I've left out all security and validation.

i retrieved the fields through array not one by one, that's why i'm having trouble with it.

Member Avatar for diafol

The principle is exactly the same - just use a loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.