Forum Moderators: coopster

Message Too Old, No Replies

Checkboxes and PHP

How to save which boxes are checked

         

dougmcc1

5:27 am on Jul 13, 2003 (gmt 0)

10+ Year Member



I have a form with many checkboxes which will get checked and unchecked often. How do I get it to remember what boxes are checked and unchecked every time I open it? I dont want to have to use a database so I'm leaning more toward fwrite().

With that, how do you make the checked checkboxes say "CHECKED" the next time the page is opened?

Thanks.

whizzy

6:00 am on Jul 13, 2003 (gmt 0)

10+ Year Member



First of all...If you have lot of traffic on that page as in lots of people using it you should consider database but the idea is the same if you use database or filesystem.

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]

dougmcc1

6:06 am on Jul 13, 2003 (gmt 0)

10+ Year Member



Only a few people are going to be using it.

Where does 'looking_for_person' come from?

How do I use the array to remember what boxes were checked?

thewebboy

6:08 am on Jul 13, 2003 (gmt 0)

10+ Year Member



You might be interested in using cookies.

whizzy

6:16 am on Jul 13, 2003 (gmt 0)

10+ Year Member



You have to read from the file and save it to the file for each time you make changes....

Try using cookies.....could be a better solution.?

dougmcc1

6:24 am on Jul 13, 2003 (gmt 0)

10+ Year Member



"You might be interested in using cookies."
But then if the cookie gets deleted, all the data is lost.

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?

dougmcc1

6:51 am on Jul 13, 2003 (gmt 0)

10+ Year Member



My if statements and everything are working now, I just need to know how to overwrite checkboxes.htm from saved.php.

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?

vincevincevince

7:20 am on Jul 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



do you have directory permissions set to allow automated file generation?
did you try fclose()'ing the file?
putting:
if (!($new_file=fopen("checkboxes.htm","w")) echo "Failed at file open";
will find if the file is never created
do you get any errors produced when you run this code? if so - can you paste them to us?

dougmcc1

8:13 pm on Jul 13, 2003 (gmt 0)

10+ Year Member



I'm running this off of my computer with Apache and I haven't gotten any "permission denied" messages.

I also don't get any other error messages when I submit, even after I added
if (!($new_file=fopen("checkboxes.htm","w"))) echo "Failed at file open";

hakre

8:27 pm on Jul 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi dougmcc1,

please enter this line of php-code in the first line, it will enable full error reporting and can help a lot while debugging:


error_reporting(E_ALL);

can you verify by file-date or content (not via the browser) wether the file has been overwritten or not?

- hakre

dougmcc1

12:34 am on Jul 14, 2003 (gmt 0)

10+ Year Member



There are no errors.

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>

The form is submitted to save.php and save.php tells me what boxes were checked. It also changes the [[]] fields to "on" if that checkbox was checked and it overwrites the original checkboxes.htm file.

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)

Here is the code in saved.php which replaces [[]] fields with "on" for the boxes which were checked and overwrites checkboxes.htm:

$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.

dougmcc1

2:33 am on Jul 14, 2003 (gmt 0)

10+ Year Member



Hmmm I just ran into another problem - what happens when the page is opened again and different boxes are clicked? the [[]] fields aren't going to be there anymore after the first time the page is submitted...

hakre

10:40 am on Jul 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi dougmcc1,

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.

dougmcc1

1:16 pm on Jul 14, 2003 (gmt 0)

10+ Year Member



Sorry, I must have misunderstood. I need it to be the same for all users. All users should see the same checked boxes and be able to make changes and save the file.

Thanks.

firemaster

3:41 am on Jul 15, 2003 (gmt 0)

10+ Year Member



This might be a longer way, I dunno? I did this for something before using a MySQL DB. I had vars in the DB and pulled them:

$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'];

then for the radio buttion (HTML w/ php insert):

<input type="radio" name="var" value="on" <? if($user_var == "yes") { echo "checked"; } else { echo "unchecked"; }?>>

expand on that and you can get it done.. of if you want to use a file then make file into an array for every \n and you can make it a read the file and you can use the code above once it is an array.. if you need help with that I can write something or someone else can..
-Mike