3

I'm a complete noob but I have searched everywhere and can't find a solution.

What I have is an array of courses that I pull from my database (e.g.: maths, art, science). These can change so I must add new courses all the time.

When a user ticks 2 of 3 courses (for example) but fails to add his username, then after the validation I want those 2 checkboxes to keep their old tick, so he must refill only his username in order to proceed.

What I get are all the checkboxes ticked :{

I'm so confused.

<?PHP
$check="unchecked";
?>

<?PHP
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    foreach ($_POST['cid'] as $cid ) {
        $check="checked";
    }
}
?>

<?PHP 
$course_data = "SELECT * FROM course ORDER BY cname";
$get_course = mysql_query($course_data) or die (mysql_error());

while ($db_field = mysql_fetch_assoc($get_course)){
    $cname= $db_field['cname'] ;//course name
    $cid= $db_field['cid'] ;// course id

    print"<BR>". 
    "<FONT COLOR='blue' SIZE='4'><B>$cname</B></FONT>".
    "<input type='checkbox'  name='cid[]' value='$cid' $check>"; // here are the courses(checkboxes)
}
?>

2 Answers 2

1

You have to set your $checked variable independently for each checkbox.

$checkedBoxes = array();
foreach($cid as $id) {
    $checkedBoxes[$id] = "checked='false'";
}

foreach ($_POST['cid'] as $cid) {
    $checkedBoxes[$cid] = "checked='true'";
}

Then in your loop that outputs the checkboxes, print the corresponding $checked value.

while ($db_field = mysql_fetch_assoc($get_course)){
    $cname= $db_field['cname'] ;//course name
    $cid= $db_field['cid'] ;// course id

    print"<BR>". 
    "<FONT COLOR='blue' SIZE='4'><B>$cname</B></FONT>".
    "<input type='checkbox'  name='cid[]' value='$cid' {$checkedBoxes[$cid]}>"; // here are the courses(checkboxes)
}
Sign up to request clarification or add additional context in comments.

1 Comment

first of all THANK YOU! but im getting an error Undefined variable: checkedBoxes in and if i remove the curly brackets after posting $checkedBoxes[$cid] i get an offset error what should i do?
1

Is this what you want?

<?PHP
if ($_SERVER['REQUEST_METHOD'] == 'POST'){

    $cid_array = $_POST['cid'];

/*foreach ($_POST['cid'] as $cid ) {
$check="checked";
}*/
}
?>


<?PHP 
$course_data = "SELECT * FROM course ORDER BY cname";
$get_course = mysql_query($course_data) or die (mysql_error());

while ($db_field = mysql_fetch_assoc($get_course)){
    $cname= $db_field['cname'] ;//course name
    $cid= $db_field['cid'] ;// course id
    if(is_array($cid_array))
{
    if(in_array($cid, $cid_array))
    {
        $check="checked='checked'";
    }
    else
    {
        $check="";
    }
}
else//it is not array because nothing was checked
{
    if($cid == $cid_array)
    {
        $check="checked='checked'";
    }
    else
    {
        $check="";
    }
}

print"<BR>". 
"<FONT COLOR='blue' SIZE='4'><B>$cname</B></FONT>".
"<input type='checkbox'  name='cid[]' value='$cid' $check>"; // here are the courses(checkboxes)
}
?>

2 Comments

Hey thanks man this is great and works but i get this Warning: in_array() expects parameter 2 to be array, any advice?
i made a slight change to my code and added a if is_array. I think that without selecting anything (on load) the array is null! If there is any problem comment and i will test locally the code and update it!

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.