Jump to content

[SOLVED] checkboxes


jb60606

Recommended Posts

I have a file (colors.txt) containing a list of colors

 

yellow

blue

red

purple

gray

 

The following code extracts the contents of the file and places it on a web page with a *checked* checkbox next to each element:

 

<?php

 

$colors = file("data/colors.txt");

sort($colors);

 

echo "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">\n";

 

foreach($colors as $color){

 

echo "<input type=\"checkbox\" value=\"{$color}\" name=\"colors[]\" CHECKED>{$color}</input><br />\n";

 

}

 

echo " <input type=\"submit\" value=\"Submit\" name=\"submit\"/>\n";

 

echo "</form>\n";

 

?>

 

I would like to be able to remove colors from the file by simply unchecking it's corresponding checkbox.

 

Anyone have any recommendations on how to do this? I've been scouring the internet for a tutorial of some sort, on handling checkbox arrays, but turned up nothing.

 

Thanks in advance.

Link to comment
Share on other sites

Not to be terribly vague here but what you're looking for are the functions fopen() and fwrite() and so on.

 

The above two functions will modify that text file for you.

 

If all fails do a web search for "deleting lines from a text file using php."

 

HTH

Link to comment
Share on other sites

thanks guys, that works perfectly.

 

I knew it would have something to do with creating a new file, overwriting the old, etc., but whatever I tried didn't work. I'm very new to the language and still struggling on a few things.

 

Thanks again.

 

 

Link to comment
Share on other sites

I think I may be going with mysql in the future though, for this project. It has gotten a little more complicated, and it just seems like it would be a lot easier working with a real database, rather than a text file. There is at least a lot more support for it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.