Jump to content

PHP Truncate + messagebox


kingzl3y

Recommended Posts

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

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>

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.

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.