jb60606 Posted June 13, 2007 Share Posted June 13, 2007 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. Quote Link to comment Share on other sites More sharing options...
trq Posted June 13, 2007 Share Posted June 13, 2007 <?php if (isset($_POST['colors'])) { $f = fopen(colors.txt,'w'); foreach ($_POST['colors'] as $color) { fwrite($f,$color); } fclose($f); } ?> Quote Link to comment Share on other sites More sharing options...
gijew Posted June 13, 2007 Share Posted June 13, 2007 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 Quote Link to comment Share on other sites More sharing options...
jb60606 Posted June 15, 2007 Author Share Posted June 15, 2007 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. Quote Link to comment Share on other sites More sharing options...
jb60606 Posted June 15, 2007 Author Share Posted June 15, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.