-2

So I have This Code:

<div class="item">
    <a href="Link to image">
  <img src="Image Source" height="268"/></a>
</div>

I want to make it so i can have a html form that says "Put Image Link Here" and then when you click submit it will add it into the top of a txt file.(above other Sections of html) So Like This:

Add Your Image:
<form>
  Raw Image File:
  <input type="text" name="firstname" value="Raw Image">
  <br>Link to image Page:
  <input type="text" name="lastname" value="Image Page">
  <br>
  <input type="submit" value="Submit">
</form>
and when you click submit it adds it to the txt and redirects you to a successful page thank you
brendan
  • 11
  • 5
  • 1
    You forgot the part where you describe a problem and ask a question. What have you tried? What isn't working? Where are you stuck? – David Jun 14 '16 at 17:48

2 Answers2

0

This is very insecure, but if you're just trying to learn the basics of how some of this stuff works, then I hope that this is a helpful demonstration.

You can use file_put_contents to write to files on disk. Be cognizant of operating system file permissions for the user account executing the script (web server). I use the /tmp directory for this example because it usually exists on linux operating systems with global read/write permissions.

Make sure to use the FILE_APPEND flag.

If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set.

<?php

$file = '/tmp/some.file.name.html';

/*
  $_GET['lastname'] is your "Link to image Page"

  please change the `name` to something more appropriate

  <input type="text" name="lastname" value="Image Page">
*/

if(isset($_GET['lastname'])){

  $string_to_write = '
    <div class="item">
        <a href="'.$_GET['lastname'].'">
      <img src="Image Source" height="268"/></a>
    </div>
  ';

  // if our file doesn't exist, then create it
  if (!file_exists($file))
    if(!touch($file))
      trigger_error('ERROR: could not create file on disk', E_USER_ERROR);

  // Write the contents to the file, 
  // using the FILE_APPEND flag to append the content to the end of the file
  // and the LOCK_EX flag to prevent anyone else writing to the file at the same time
  if (!file_put_contents($file, $string_to_write, FILE_APPEND | LOCK_EX))
    trigger_error('ERROR: could not write to file on disk', E_USER_ERROR);

  // redirect to wherever
  header('Location: /');

}

// print the file to screen
include($file);

?>

Add Your Image:
<form>
  Raw Image File:
  <input type="text" name="firstname" value="Raw Image">
  <br>Link to image Page:
  <input type="text" name="lastname" value="Image Page">
  <br>
  <input type="submit" value="Submit">
</form>

I'll leave the "Image Source" part for you. Uploading files is a different question.

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
  • Thank you but now my question is you know when it adds the html, it adds it to the bottom of the the list. is there a way to to have it add the new html to the top of the document. but any way thanks jeff – brendan Jun 15 '16 at 00:39
  • that's an excellent [follow-up question](http://meta.stackexchange.com/questions/19457/where-should-i-post-follow-up-questions). Try working on it as best you can, and if you get stuck, then you should post another question :) But first, you should see if [someone has already asked that question](http://stackoverflow.com/q/3332262/4233593) <--hint – Jeff Puckett Jun 15 '16 at 02:26
  • @brendan of those, I think you will be mostly interested in [this one](http://stackoverflow.com/a/3332302/4233593). – Jeff Puckett Jun 15 '16 at 02:28
-1
<?php
 if (isset($_POST['link']))
   { echo "Your link is ".$_POST['link']; }
    /* header('Location: url');  */
     /* uncomment the above line for redirect */   
 ?>
<html>
  <head>
   <title>Sample Application</title>
  </head>
 <body>
   <br>Add Your Image:
    <form action="index.php" method="post">
     Link to Image page: <input type="text" name="link">
     <input type="submit" value="Submit">
    </form>
 </body>
</html>

If i understood your question correctly, you are trying to add URL (Entered by user) to the HTMl text, on clicking submit.
use header for redirecting

header('Location: url');
ravindar
  • 406
  • 4
  • 13
  • This is not a solution because you cannot both display the link and redirect. OP says *"add it into the top of a txt file"*, and your method will only display the link one time when it's posted. – Jeff Puckett Jun 14 '16 at 18:37