0

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/

1
  • Wouldn't it be better to have a php input information that is saved somewhere (text file, database, or elsewhere) that you then extract with another php file? Commented Apr 23, 2016 at 1:39

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Sentences begin with capital letters.
so Amended, thank You for an irrelevant Comment in a programming Forum. @ Lightness Races in Orbit
0

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!

2 Comments

@gavgrif edited, thanks for the heads up! Semi colons are literally my most frequent typo's, not just by missing them but my accidentally using a colon, especially since I often am using Shift+0 to create the ')' right before the semi colon.
no probs - @Webring - did like your answer though - this method in action.php would also be great for writing errors to error.log.txt

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.