Leon
Leon

Reputation: 45

Different HTML files in one PHP file

I searched in the forum for about 2 hours and didn't find a solution for that.

Problem:

I have one PHP file. And I have multiple HTML files.

At first I show the first HTML file when the PHP file is loaded. So I included the code from "page1.html" into the PHP file.

Now after clicking the submit button and the validation on this PHP file I want to replace the current HTML file with another one.

But when I include page2.html after the validation was successful, it shows just the body content of the page2.html file under the "page1.html" content.

I want to replace the entire html file, not just a few elements. And it has to be in the PHP file where the first file was included too.

Is there any solution?

<?php



include 'page1.html';


// define variables and initialize with empty values

$Content "";
$Error = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
    if (!isset($_POST["name"]))
        {
        $Error = "There is an error.";
        }
      else
         {
        $Content = $_POST["content"];
        }


    if (empty($Error))
        {

        include 'page2.html';


        }
    }

?>

Upvotes: 0

Views: 46

Answers (2)

Cameron White
Cameron White

Reputation: 9

Once the user has clicked submit on page1.html a form is sent to the server to be validated by the php file. Assuming that validation is successful you can redirect the browser to page2.html using header("Location:page2.html"); which will set the location in the response headers to page2.html.

Upvotes: 1

Al Amin Chayan
Al Amin Chayan

Reputation: 2500

Use condition in your PHP file

<?php

if (isset($_POST))
{
    include 'page2.html';
}
else
{
    include 'page1.html';
}

Upvotes: 0

Related Questions