Jump to content


Help


6 replies to this topic

#1 kingzl3y

    Young Padawan

  • Members
  • Pip
  • 16 posts

Posted 01 July 2007 - 08:35 AM

Well, i've started to use this guestbook script.
It doesn't use a database, it just uses normally txt files to save the entries in.
I want to clear the guestbook when ever it gets spammed etc (or well, thats not really the problem, just so i can learn some PHP)
I've read up on it..and came up with this script
<?php
 
 $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>";
 }
 ?>
It's my first php script so it might not be 100%correct.

The question is.
Is there anyway i can create a button which would then call the php function which wouild then clear the txt file? if that makes sense?

#2 Chaos King

    Senior Programmer

  • P2L Staff
  • PipPipPip
  • 676 posts
  • Gender:Male
  • Location:Florida

Posted 01 July 2007 - 08:42 AM

Sure thing, you can just attach it to a form button.

Lets say that the script you have above is in a file called clear.php

Just have this button in your administrative panel or something:

<form action="clear.php" method="post">
<input type="submit" value="Clear" />
</form>


#3 kingzl3y

    Young Padawan

  • Members
  • Pip
  • 16 posts

Posted 01 July 2007 - 11:43 AM

Oahw sweet!
But, is it possible to like have like erm...i don't know really how to word this.

Is there like anyway inwhich i can have that php function (function??) in the same document as the button. And when the button is clicked it actually tells me that the file has been cleared in the same document..(gawd i hope that makes sense)
Kinda like

button-Clear
*clicks*
<h1>File has been cleared</h1>

etc?

#4 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 01 July 2007 - 02:12 PM

If you were wanting instant working, and have no page reload in-between, you would use AJAX.

Otherwise, you can simply make it all one file.

<?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" value="Clear" />
</form>


#5 kingzl3y

    Young Padawan

  • Members
  • Pip
  • 16 posts

Posted 02 July 2007 - 09:33 AM

Thnx!
I'm not really that fused on a page reload or not.
And that code you provided doesn't work :mellow:
I'm not that good at AJAX or JS for that fact, :\

#6 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 02 July 2007 - 11:08 AM

My fault, forgot about the name attribute.
Something I thought you'd catch, it's simple HTML basics there.

<?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>


#7 kingzl3y

    Young Padawan

  • Members
  • Pip
  • 16 posts

Posted 03 July 2007 - 04:49 AM

Lol thnx alot.
Yeah sorry, didn't really actually read the code just pasted it in to try, yesterday was a busy day so not alot of time.
But thnx alot, works great.

:angrylooking:





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users