kingzl3y Posted July 15, 2007 Share Posted July 15, 2007 Hey, Firstly I'm new to PHP, so don't rofl if i sound a wee bit stupid :> Secondly, I'm creating a personal website for myself, on this website i have a guestbook, which is textfile driven (no db's just pure .txt's). I've realised most guestbooks get spammed really easy, so i want to create a button which when pressed will 'truncate' the .txt and make it blank. I've already came up with this: <?php if(isset($_POST['submit']) && $_POST['submit'] == 'Clear'){ $filename = 'test.txt'; if (is_writable($filename)) { if (!$handle = fopen($filename, 'w')) { echo "<h1>Cannot open file ($filename)</h1>"; exit; } echo "<h1>Success, $filename has been cleared </h1>"; fclose($handle); } else { echo "<h1>The file $filename is not writable</h1>"; } } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="submit" name="submit" value="Clear" /> </form> But when i click the button, it doesn't show the Headings (echo "<h1> blah blah") it just doesn't do anything. So i had a go at fixing it...couldn't so i decided to choose JavaScript to show a messagebox instead of echo'ing e.g. File not writable > messagebox " File not writable" etc. I'm not sure how to use JS in PHP so i google'd After a few queries i came up with this: <?php if(isset($_POST['submit']) && $_POST['submit'] == 'Clear'){ $filename = 'test.txt'; if (is_writable($filename)) { if (!$handle = fopen($filename, 'w')) { echo "<script language=\"JavaScript\">\n"; echo "window.alert("Yes");\n"; echo "</script>"; exit; } echo "<script language=\"JavaScript\">\n"; echo "window.alert("NO");\n"; echo "</script>"; fclose($handle); } else { echo "<script language=\"JavaScript\">\n"; echo "window.alert("Maybe");\n"; echo "</script>"; } } ?> <br /> <input type="submit" name="submit" value="Clear" /> </div> Thought it would of worked....you guessed, it didn't :> i try to access the page and get this: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/kingz/public_html/kingzl3y.co.uk/manager.php on line 60 Help pleassssssssssssse. Liefejè! Link to comment https://forums.phpfreaks.com/topic/60114-php-truncate-messagebox/ Share on other sites More sharing options...
pocobueno1388 Posted July 15, 2007 Share Posted July 15, 2007 This will fix your error messages: <?php if(isset($_POST['submit']) && $_POST['submit'] == 'Clear'){ $filename = 'test.txt'; if (is_writable($filename)) { if (!$handle = fopen($filename, 'w')) { echo "<script language=\"JavaScript\">\n"; echo "window.alert('Yes');\n"; echo "</script>"; exit; } echo "<script language=\"JavaScript\">\n"; echo "window.alert('NO');\n"; echo "</script>"; fclose($handle); } else { echo "<script language=\"JavaScript\">\n"; echo "window.alert('Maybe');\n"; echo "</script>"; } } ?> <br /> <input type="submit" name="submit" value="Clear" /> </div> Link to comment https://forums.phpfreaks.com/topic/60114-php-truncate-messagebox/#findComment-299025 Share on other sites More sharing options...
kingzl3y Posted July 15, 2007 Author Share Posted July 15, 2007 Awesome! Fast reply! BUT! (stupid buts ><) It isn't working.. i think there is a problem with the truncate part, it just doesn't do anything.. doesn't delete the contents in text.txt nor does it display a message box. Is my code correct? Link to comment https://forums.phpfreaks.com/topic/60114-php-truncate-messagebox/#findComment-299034 Share on other sites More sharing options...
pocobueno1388 Posted July 15, 2007 Share Posted July 15, 2007 You never told it to delete anything... <?php if (isset($_POST['submit'])) { $filename = 'test.txt'; if (is_writable($filename)) { if ($handle = fopen($filename, 'w')) { fputs($handle, ""); fclose($handle); echo "<h1>Cleared</h1>"; } else { echo "ERROR: Couldn't clear"; } } else { echo "This file isn't writtable!"; } } ?> <form method='post'> <input type="submit" name="submit" value="Clear" /> </form> See if that does what you want. Link to comment https://forums.phpfreaks.com/topic/60114-php-truncate-messagebox/#findComment-299052 Share on other sites More sharing options...
trq Posted July 15, 2007 Share Posted July 15, 2007 Your code would be much easier to read if you get the indentation right. Also... simple, to the point messages are often more helpfull during debugging. The problem however is most likely the fact that you had no <form> defined. <?php if (isset($_POST['submit']) && $_POST['submit'] == 'Clear') { $filename = 'test.txt'; if (is_writable($filename)) { if (!$handle = fopen($filename, 'w')) { echo "Unable to open file"; } else { echo "Success"; fclose($handle); } else { echo "File not writtable"; } } ?> <br /> <form method='post'> <input type="submit" name="submit" value="Clear" /> </form> </div> Link to comment https://forums.phpfreaks.com/topic/60114-php-truncate-messagebox/#findComment-299054 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.