I have a problem on using php array for checkbox by using foreach

Member Avatar for diafol

Post your code maybe?

Dear mascot07,

Post your code first, so we can check where is the problem.

yea why wouldn't you post the code.
probably a simple mistake

well here is my problem (go easy im a complete noob :D)

this is where i bring out from my database some courses
checkboxes are empty.
i want after clicking a checkbox to retain the tick after submitting a form
thanks in advance

<?PHP
while ($db_field = mysql_fetch_assoc($get_course)){
$cname= $db_field['cname'] ;
$cid= $db_field['cid'] ;


print"<BR>". 
"<FONT COLOR='blue' SIZE='4'><B>$cname</B></FONT>".
"<input type='checkbox'  name='cid[]' value='$cid'  >";


}
?>
Member Avatar for diafol

ARe you sending the form to itself (same page) or to another page for processing?

If you want to keep checks, you can do one of 3 main things:

1) Keep values of checks from $_POST if sending to itself
2) Keep values of checks in session or cookie if sending to a different page
3) Use ajax submit, so that form isn't refreshed.

Solution 1):

<?php
$check1 = '';
$check2 = '';

if(isset($_POST['check1']))$check1 = ' checked="checked"';
if(isset($_POST['check2']))$check2 = ' checked="checked"';
?>

<form method="post">
    <input id="check1" name="check1" type="checkbox"<?php echo $check1;?> />
    <input id="check2" name="check2" type="checkbox"<?php echo $check2;?> />
    <input id="submit" name="submit" type="submit" value="Go"  />
</form>

Solution 2):

//formhandler.php
session_start();
if(isset($_POST['submit'])){
    $_SESSION['form_values'] = $_POST;
}
//do processing
header("Location: form.php");

//form.php

<?php
session_start();
$check1 = '';
$check2 = '';

if(isset($_SESSION['form_values']['check1']))$check1 = ' checked="checked"';
if(isset($_SESSION['form_values']['check2']))$check2 = ' checked="checked"';
if(isset($_SESSION['form_values']))unset($_SESSION['form_values']);
?>

<form method="post" action="form_handler.php">
    <input id="check1" name="check1" type="checkbox"<?php echo $check1;?> />
    <input id="check2" name="check2" type="checkbox"<?php echo $check2;?> />
    <input id="submit" name="submit" type="submit" value="Go"  />
</form>

Hi sorry for not explaining it well
in my database i have 3 courses
so i pull it out.
Submitted on same page

maths || <--- checkboxes :D
art ||
science ||

<?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'] ;     //its id


print"<BR>". 
"<FONT COLOR='blue' SIZE='4'><B>$cname</B></FONT>".

"<input type='checkbox'  name='cid[]' value='$cid'  >";//Here are the courses with a checkbox


}
?>

and so if i submit the form i have maths and art ticked but the user forgot to enter his name so normally the page would like refresh and the ticks would go away
so how do i keep those 2 ticked (and its in 1 array so im thinking it has something to do with foreach but im just so burnt out i cant think ^^)

Member Avatar for diafol

I can't make any sense of what you're trying to do - sorry - anybody else?

I'm thinking if what he wants is a checkbox that takes effect upon selection, without a submit button..

ok i have a form right and i must validate it
so
it has username and you must tick checkboxes that are pulled from the database
now the user ticks 2/3 checkboxes right
but does not enter his name
and of course the ticks go away after you hit the submit button
so how do i keep those 2 ticks on the checkboxes

diafol i know how to do the above examples but mine is directly called from the database
so its an array

hope now i make sense ? :D

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.