Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php submit link

Status
Not open for further replies.

Unsalted

Technical User
Jul 28, 2006
25
GB
Im looking at having a form where users can type in a web address and it gets written to a txt file then updates the list on the page,

I can just about manage to do it with an mysql database, but I was wondering how easy it is to do it where it just writes to a simple txt file, this is something I have never tried,

any tips would be appreciated

thanks
 
try something like this (not tested so there may be syntax errors).

Code:
<?
echo <<<STR
<form action="{$_SERVER['PHP_SELF']}" method="post">
<input name="text" type="text" />
<input name="submit" type="submit" value="post"/>
</form>

STR
$filename = "somefile.txt";
if (isset($_POST['text'])):
  $fh = fopen($filename, "abt") or die ("cannot open file for writing");
  @flock($fh, LOCK_EX);
  fwrite($fh, $_POST['text']);
  fclose($fh);
endif;
$fcontents = file_get_contents($filename) or die ("cannot open file for writing");
echo "<hr/><br/>Current data is<br/>";
echo nl2br($fcontents);
?>
 
syntax errors: ... found one ... add a semicolon after the STR in the middle of the script
 
wow, Ill try this! and get back to you,

many thanks!
 
I cant see where the semicolon is missing?
 
Ok this seems to work great! thanks a million,

Its writing ok the txt file,

now all I need is to make each one a link by echoing a ahref before each entry,

and make sure they go on a new line, any ideas? I put a break in after the $fcontents, but it didnt work
 
change
Code:
 fwrite($fh, $_POST['text']);
tp
Code:
 fwrite($fh, "<a href=\"{$_POST['text']}\">{$_POST['text']}</a>\r\n");

that should do it.
 
that is surprising. have a look inside the datafile itself and see whether the data is being correctly written. if it looks odd in the file too then try changing this line as shown:
Code:
fwrite($fh, "<a href='{$_POST['text']}'>{$_POST['text']}</a>\r\n");
 
i've tested this code (as below) and it works fine. it does not give me the format errors that you are reporting so i suspect that your copy and paste has gone wrong:

Code:
<?
echo <<<STR
<form action="{$_SERVER['PHP_SELF']}" method="post">
<input name="text" type="text" />
<input name="submit" type="submit" value="post"/>
</form>

STR;
$filename = "somefile.txt";
if (isset($_POST['text'])):
  $fh = fopen($filename, "abt") or die ("cannot open file for writing");
  @flock($fh, LOCK_EX);
  fwrite($fh, "<a href=\"{$_POST['text']}\">{$_POST['text']}</a>\r\n");
  fclose($fh);
endif;
$fcontents = file_get_contents($filename) or die ("cannot open file for writing");
echo "<hr/><br/>Current data is<br/>";
echo nl2br($fcontents);
?>
 
The text file itself is being written to as it should.

i.e

the entries are

Code:
<a href="[URL unfurl="true"]www.theaddedsite.com">www.theaddedsite.com</a>[/URL]
 
and I now understand pretty much , what is going on in the script apart from echo <<<STR ? whats that for?
 
that's the heredoc syntax. just a different way or writing string values
 
Unless you add a protocol at the beginning of the link, browser will assume you're beginning your url from the current location when the script is run. So if you're running this file at the browser will simply add the href value to that, since no protocol was given and the link will be If you want to change that, you will need to append the protocol, that is http:// at the beginning of the links. Even better is, you should check if the person already put the protocol in and if the link begins with leave that and if not add it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top