6
Contributors
9
Replies
10
Views
5 Years
Discussion Span
Last Post by themastermind
0

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'  >";


}
?>

Edited by themastermind

0

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

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

0

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

0

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

Edited by themastermind

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.