user5544792
user5544792

Reputation: 53

Php to add to html file

How can I use PHP to edit a html file on a website? To explain what I want to do, I want to have a php file with input boxes to add to a list in html.

So admin.php will edit index.html by adding more contents to the list.

<body>
<li>
<ul>Dog</ul>
<ul>Cat</ul>
</li>
</body>

https://jsfiddle.net/dam30t9p/

Upvotes: 0

Views: 65

Answers (2)

Webeng
Webeng

Reputation: 7143

As I mentioned in my comment, I recommend you create a form that then saves this information (in a database, text file, or other storage option), and then another php file would extract that information. Since I believe you are new to programming, I will explain how to do it with a text file, however I HIGHLY recommend you use a database to store the information, not just because of it's speed in executing queries, but for securely storing information that might be sensative.

Form page: Index.php

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <form method="POST" action="action.php">
      <input type="text" name="message1">
      <input type="text" name="message2">
      <input type="submit" value="Submit">
    </form>
  </body>
</html>

Php page that saves the information: action.php

<?php

//receiving the values from the form:
//I also used htmlspecialchars() here just to prevent cross
//site scripting attacks (hackers) in the case that you 
//echo the information in other parts of your website
//If you later decide to store the info in a database,
//you should prepare your sql statements, and bind your parameters
$message1 = htmlspecialchars($_POST['message1']);
$message2 = htmlspecialchars($_POST['message2']);

//saving the values to a text file: info.txt
$myFile = fopen('info.txt', 'w');
fwrite($myFile, $message1."\n");
fwrite($myFile, $message2."\n");
fclose($myFile);

?>

Then in another php file you would retrieve that information and use it in your website:

page2.php

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <?php

      $myFile = fopen('info.txt', 'r');
      $message1 = fgets($myFile);
      $message2 = fgets($myFile);
      fclose($myFile);

      echo "message1 = ".$message1;
      echo "message2 = ".$message2;

    ?>
  </body>
</html>

Let me know if that helped!

Upvotes: 0

gavgrif
gavgrif

Reputation: 15509

You should use a database to store the contents and then using php - extract the contents out of the db and echo them into the page.

Also you need to swap the ul's and li's in your document:

    <body>
        <ul>
           <li>Dog</v>
           <li>Cat</li>
        </ul>
    </body>

for example:

    <body>
        <ul>
          <?php
              //method to extract data from the db giving a variable called $content
              foreach($rows as $row //whatever loop you created)
                {
                  $content=$row['content'];
                  echo"<li>".$content."</li>";
                }
            ?>
        </ul>
    </body>

Upvotes: 1

Related Questions