spillage Posted April 10, 2008 Share Posted April 10, 2008 You will have to forgive me because all this is very new to me. I have a .txt file recieving data from a form page and another page where I can view the data in a table. What I am trying to do is create a drop down menu to delete or save the data and a submit button on the table page. If delete is submited then I need to data deleted but I'm using unlink and this deletes the entire file and then brings up error because the is no file to pull in when the page is refreshed. If there is no drop down menu and no data on the file then "no data a present" is shown. Thinking of using if (file_exists("file name")) echo '$a' else echo 'no data' . But $a would need to be enire code for page and I cannot work out how this would work. Sorry if this makes no sense. Thanks Mark Link to comment https://forums.phpfreaks.com/topic/100477-deleting-data-from-a-file/ Share on other sites More sharing options...
rhodesa Posted April 10, 2008 Share Posted April 10, 2008 instead of unlink, you can just open the file in 'w' mode, then close it. this will clear the file without deleting it <?php fclose(fopen('filename.txt','w')); ?> Link to comment https://forums.phpfreaks.com/topic/100477-deleting-data-from-a-file/#findComment-514128 Share on other sites More sharing options...
spillage Posted April 10, 2008 Author Share Posted April 10, 2008 Many thanks rhodesa could anyone just check my code as all that happens now when the page is opened is all the data is deleted. Think I need a good couple of drinks as driving me mad. Not even sure I need to $_POST. Was fine using options that post to another page but now the option box is on the same page I seem to have lost the plot and cant get my head around it. Many thanks Mark <tr> <td><h3> Would you like to keep orders ?</h3></td> <td><select name="delete"> <option value = "a"> Yes</option> <option value = "b">No</option> </select> </td> </tr> <tr bgcolor="#4169e1"> <td colspan="2" align="center"><input type="submit" value="Submit"></td> </tr> </form> </html> <?php $delete= $_POST["delete"]; $removefile = fclose(fopen("$DOOCUMENT_ROOT/ordering.txt",'w')); if ($delete == "b") { echo $removefile; } Link to comment https://forums.phpfreaks.com/topic/100477-deleting-data-from-a-file/#findComment-514151 Share on other sites More sharing options...
rhodesa Posted April 10, 2008 Share Posted April 10, 2008 That will always remove the file. Move the fopen/fclose inside the IF statement: <?php $delete= $_POST["delete"]; if ($delete == "b") { $removefile = fclose(fopen("$DOOCUMENT_ROOT/ordering.txt",'w')); print 'Emptied'; } ?> Link to comment https://forums.phpfreaks.com/topic/100477-deleting-data-from-a-file/#findComment-514155 Share on other sites More sharing options...
spillage Posted April 10, 2008 Author Share Posted April 10, 2008 many many thanks Link to comment https://forums.phpfreaks.com/topic/100477-deleting-data-from-a-file/#findComment-514211 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.