...

View Full Version : keep checkbox checked



speedy_rudolf
10-27-2009, 11:40 AM
Hi. I'm having a pretty hard time figuring how to do something... In a nutshell, I have a form, when submitted it calls a php page that emails me the form, if it can't email it, it stores the form field values in a txt file, then calls up another php page, which is actually the form with the fields already populated (the field values are read from the file and assigned to the respective fields). What I'm having trouble with is some checkboxes and radios: they already have values assigned to them, so I don't know how to check them if they were previously checked.
Anyway...maybe part of the code will clear things up a bit:

<?php
$fis_txt=fgets($eroare, 102400);
$text_txt=fgets($eroare, 102400);
$nav_sel=fgets($eroare, 102400);
for($i=0; $i<22; $i++)
{$navs.$i=fgets($eroare, 102400);}
?>

<html>
<input type="radio" name="Text" value="Fisier" onClick="Val_text();Cont_pag2();">Check the one that was previously checked
<input type="radio" name="Text" value="Text" onClick="Val_text();Cont_pag2();">Check the one that was previously checked
<input type="file" name="Fis_txt" size="100" value="<?php echo $fis_txt; ?>" onChange="Cont_pag2()">
<textarea name="Text_txt" cols="80" rows="10" value="<?php echo $text_txt; ?>" onKeyUp="Cont_pag2()">

<input type="checkbox" id="Navs0" name="Navs0" value="1" onClick="Cont_pag5_3()">Check it if it was previously checked, if not leave it unchecked
<input type="checkbox" id="Navs1" name="Navs1" value="1" onClick="Cont_pag5_3()">Check it if it was previously checked, if not leave it unchecked
<input type="checkbox" id="Navs2" name="Navs2" value="2" onClick="Cont_pag5_3()">Check it if it was previously checked, if not leave it unchecked
And so on until Navs21
</html>

Well...like I said, this is just a small part of the code...the entire file has about 80KB...
Anyway...If the checkbox was previously checked, in the txt file the respective line would say ON, if it was unchecked it would say OFF. And for the radios, the txt file would contain the value of the one that was checked.

So...How do I go and do that? Any help will be very much appreciated...

abduraooft
10-27-2009, 11:44 AM
What I'm having trouble with is some checkboxes and radios: they already have values assigned to them, so I don't know how to check them if they were previously checked. How do you know whether a check box was checked or not? Any clue left in the variable $navs.$i ?

speedy_rudolf
10-27-2009, 12:02 PM
How do you know whether a check box was checked or not? Any clue left in the variable $navs.$i ?
I already said...if the checkbox was checked, the respective line would contain the word "ON", so $navs.$i would have the value "ON". If it wasn't checked, the line (and ultimately $navs.$i) would contain the word "OFF".

abduraooft
10-27-2009, 12:45 PM
Try
<?php
for($i=0; $i<22; $i++)
{
$navs.$i=fgets($eroare, 102400);
$checked=($navs.$i=='On')?'checked="checked"':'';

echo "<input type=\"checkbox\" {$checked} id=\"Navs{$i}\" name=\"Navs{$i}\" value=\"{$i}\" onClick=\"Cont_pag5_3()\">";

}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum