Jump to content

Automaticly Create a Link From a Submit Form?


Chicken

Recommended Posts

FORM.HTML

<form action="pagewithlinks.php" method="post">
    <input type="text" name="newlink"></input>
    <input type="submit" value="Subscribe"></input>
</form>

 

PAGEWITHLINKS.PHP

<?php
$link = $_POST['newlink'];
echo "<a href='$link'>LINK TO WHATEVER YOU TYPED IN</a>";
?>

FORM.HTML

<form action="pagewithlinks.php" method="post">
    <input type="text" name="newlink"></input>
    <input type="submit" value="Subscribe"></input>
</form>

 

PAGEWITHLINKS.PHP

<?php
$link = $_POST['newlink'];
echo "<a href='$link'>LINK TO WHATEVER YOU TYPED IN</a>";
?>

 

It overwrites the one before it...

You didn't ask for the links to be added to a database, but it sounds like you will need to write a php program that adds links to a database, then when the pagewithlinks.php page is viewed you will query the database and echo the result of the query. This is about as simple of a task as you can do with php/mysql. I recommend reading this book to learn how to do this:

 

PHP & MySQL For Dummies 3rd edition

Here you go. No mysql database using fwrite. happy trails.

 



<?php

if(isset($_REQUEST['submit'])){

$filename="links.txt";

$somecontent="****$_REQUEST['links']****";

$handle = fopen("$filename", "ab");

   if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
   }

   echo $somecontent." has been written to ".$filename;
   fclose($handle);
}

?>

<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<label>Links</label><input type='text' name='links'><br />
<input type='submit' name='submit' value='Submit'><br />
</form>



Here's the code to retrieve the data and display it.

<?php

$filename="links.txt";


if(file_exists($filename) && filesize($filename) > 0){

$fh = fopen($filename, 'rb');
$thedata = fread($fh, filesize($filename));
fclose($fh);
$break=explode("****",$thedata);

foreach($break as $value){

echo $value."<br />";

}


}
?>

Here you go. No mysql database using fwrite. happy trails.

 



<?php

if(isset($_REQUEST['submit'])){

$filename="links.txt";

$somecontent="****$_REQUEST['links']****";

$handle = fopen("$filename", "ab");

   if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
   }

   echo $somecontent." has been written to ".$filename;
   fclose($handle);
}

?>

<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<label>Links</label><input type='text' name='links'><br />
<input type='submit' name='submit' value='Submit'><br />
</form>



Here's the code to retrieve the data and display it.

<?php

$filename="links.txt";


if(file_exists($filename) && filesize($filename) > 0){

$fh = fopen($filename, 'rb');
$thedata = fread($fh, filesize($filename));
fclose($fh);
$break=explode("****",$thedata);

foreach($break as $value){

echo $value."<br />";

}


}
?>

 

Will it write to the middle of a file without overwriting previous data?

  • 2 weeks later...

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.