script submits twice

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DaRk
Forum Newbie
Posts: 4
Joined: Tue Aug 03, 2004 9:41 am

script submits twice

Post by DaRk »

I have a script that lets the user submit his link and then puts it in a file. that file is the read and shown on to the appropiate page. But if press ed submit it submits the previous entry and the entry of the current user. How do I fix it?

Here is the code:

Code: Select all

<?
require_once("links.php");

if ($_POST == "submit") {
	$file = "links.php";
	$name = $HTTP_POST_VARS["name"];
	$url = $HTTP_POST_VARS["url"];

	$fp = fopen ($file, 'a+');
	
	$string = '<a href="'.$url.'/" target="_blank">'.$name.'</a><br>\n';
	
	fwrite ($fp,$string);
	fclose ($fp);
}
?>
<form method="post" action="<?php echo($PHP_SELF); ?>">
Name of your site: <input type="text" name="name">
URL: <input type="text" name="url">
<input type="submit" name="submit" value="sumbit">
</form>
</p>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

some of your code needs updating and some of it is wrong

Code: Select all

<? 
require_once("links.php"); 

if ($_POST['submit'] == "submit") { 
   $file = "links.php"; 
   $name = $_POST['name']; 
   $url = $_POST['url']; 

   $fp = fopen ($file, 'a+'); 
    
   $string = '<a href="'.$url.'/" target="_blank">'.$name.'</a><br>\n'; 
    
   fwrite ($fp,$string); 
   fclose ($fp); 
} 
?> 
<form method="post" action="<?php echo($_SERVER['PHP_SELF']); ?>"> 
Name of your site: <input type="text" name="name"> 
URL: <input type="text" name="url"> 
<input type="submit" name="submit" value="sumbit"> 
</form>
Post Reply