Forum Moderators: coopster
With that, how do you make the checked checkboxes say "CHECKED" the next time the page is opened?
Thanks.
You create the checkboxes with the same name and after the name you make a []. Like this:
<input name=car[] type=checkbox value="Ford" >
<input name=car[] type=checkbox value="Opel" >
Then when the user post the form you can pic up in an array what boxes is checked:
$car_array = $_POST['car'];
Then you need to loop through the array reading the values to a file you have set
for ($i=0; $i<count($car_array); $i++) {
// write $per_array[$i] to file
}
Hope this helps a bit...
[edited by: whizzy at 6:13 am (utc) on July 13, 2003]
I'm thinking of something like this:
checkboxes.htm:
<form method=post action=save.php>
::check1 (name=check1 value="1 is checked")
::check2 (name=check2 value="2 is checked")
::check3 (name=check3 value="3 is checked")
::check4 (name=check4 value="4 is checked")
button type=submit value="Save Changes"
</form>
save.php:
if ($_POST['check1'] == "1 is checked")
{
insert CHECKED into the checkbox tag
(same with check2, check3, and check4)
}
then overwrite checkboxes.htm
Will that work? If so, can someone post the code for that please?
In saved.php, this isn't working:
# Open the file
$checkboxes=fopen("checkboxes.htm","r");
# Read page into variable
$checkboxes=fread($checkboxes,102400);
# Create a new file to write the changes to and overwrite the existing page
$new_file=fopen("checkboxes.htm","w");
fwrite($new_file,$checkboxes);
What am I doing wrong?
I'm so close to getting it to work. Here is my form code:
<form method=post action="saved.php">
1. client1<br>
good client? <INPUT NAME="yes1" TYPE="checkbox"[[yes1]]> yes
<INPUT NAME="no1" TYPE="checkbox"[[no1]]> no
<br><br>
2. client2<br>
good client? <INPUT NAME="yes2" TYPE="checkbox"[[yes2]]> yes
<INPUT NAME="no2" TYPE="checkbox"[[no2]]> no
<br><br>
<INPUT NAME="SUBMIT" TYPE="submit" VALUE="Save Changes">
</form>
So instead of it writing "on", I need it to write " CHECKED" so that when checkboxes.htm is overwritten, the checkbox gets checked.
Now here is the if statements in my saved.php file which tells me what boxes were checked:
if ($_POST['yes1'] == "on")
{
echo "The <b>if</b> statement says yes1 is checked for client1<BR>";
$yes1 = " CHECKED";
}
else echo "The <b>if</b> statement says yes1 isn't checked for client1<BR>";
$yes1 = "";
if ($_POST['no1'] == "on")
{
echo "The <b>if</b> statement says no1 is checked for client 1<BR><BR>";
$no1 = " CHECKED";
}
else echo "The <b>if</b> statement says no1 isn't checked for client 1<BR>";
$no1 = "";
(same for yes2 and no2)
$form=fopen("checkboxes.htm","r");
$form=fread($form,102400);
$fields_to_parse=array("yes1","no1","yes2","no2");
while (list($null,$field)=each($fields_to_parse)) {
$form=str_replace("[[".$field."]]",strip_tags($_POST[$field]),$form);
if ($new_file=fopen("checkboxes.htm","w")) {
fwrite($new_file,$form);
fclose($new_file);
} else {
die("There was an error in overwriting the file");
}
}
It isn't even using $yes1, $no1, etc. in the if statements but that might be an option.
that was one of the earlier posts in this thread. do you want to have the pre-selection for all users or do you want to use them for a specific user only. if it's for a specific user, you'll need a session handling mechnism (php4 has this built in). this can be cookie based.
so what is what you're looking for:
a) default-checked for all users
b) defailt-checked for a specific user
? - hakre.
$m_query = "SELECT * FROM table";
$ms_query = mysql_query($m_query) or die (mysql_errno().": ".mysql_error()."<BR>");
$row = mysql_fetch_array($ms_query);
$user_var = $row['var'];
<input type="radio" name="var" value="on" <? if($user_var == "yes") { echo "checked"; } else { echo "unchecked"; }?>>