Summary: https://www.lootjeapp.nl/Lootje/producedAndSavedByPHPAndSomethingWrong.html is what my .php file created, and it is not displayed right.
However, when I copy it's source code and put it into a new .html file, it works like a charm: https://www.Lootjeapp.nl/Lootje/sourceOfErroredPage.html
Everything looks fine, and apparently it's source code is fine so Why won't the original work?
I created a .php page that can take a $POST request and then creates a brand new .html page according to the variables received.
<?php
$onderwerp = $_POST['onderwerp'];
$Speler1 = $_POST['Speler1'];
$Speler2 = $_POST['Speler2'];
$string1 = " background-image: url('https://www.lootjeapp.nl/forestbridge.jpg');";
//We use the input to create a new page
$newPage="../../Lootje/$onderwerp$Speler1$Speler2.html";
//Maybe naming the page this way creates a problem for browsers to fully recognise it as html
$fh = fopen($newPage, 'w');
//below writes the html code for the new page
fwrite($fh,"<!DOCTYPE html>\n");
fwrite($fh,"<html>\n");
fwrite($fh,"<title>W3.CSS Template</title>\n");
fwrite($fh,'<meta charset="UTF-8">');
fwrite($fh,'<meta name="viewport" content="width=device-width, initial-scale=1">');
fwrite($fh,'<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">');
fwrite($fh,'<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">');
fwrite($fh,'<style>');
fwrite($fh,'body,h1 {font-family: "Raleway", sans-serif}');
fwrite($fh,'body, html {height: 100%}');
fwrite($fh,'.bgimg {');
//the fwrite syntax had a problem because the syntax for the background image file was seen as closing the argument, hence the $string1 introduction
fwrite($fh,$string1);
fwrite("\n");
fwrite($fh,' min-height: 100%;');
fwrite($fh,' background-position: center;');
fwrite($fh,' background-size: cover;');
fwrite($fh,"\n}");
fwrite($fh,'</style>');
fwrite($fh,'<body>');
fwrite($fh,"\n");
fwrite($fh,'<div class="bgimg w3-display-container w3-animate-opacity w3-text-white">');
fwrite($fh,' <div class="w3-display-topleft w3-padding-large w3-xlarge">');
fwrite($fh,"\n Logo");
fwrite($fh,"\n </div>\n");
fwrite($fh,' <div class="w3-display-middle">');
fwrite($fh,' <h1 class="w3-jumbo w3-animate-top">LOOTJE</h1>');
fwrite($fh,' <hr class="w3-border-grey" style="margin:auto;width:40%">');
fwrite($fh,' <p class="w3-large w3-center">35 days left</p>');
fwrite($fh,' </div>');
fwrite($fh,' <div class="w3-display-bottomleft w3-padding-large">');
fwrite($fh,' Powered by <a href="https://www.w3schools.com/w3css/default.asp" target="_blank">w3.css</a>');
fwrite($fh,' </div>');
fwrite($fh,'</div>');
fwrite($fh,'</body>');
fwrite($fh,'</html>');
fclose($fh);
?>
The good news is that the new .html file does get created and the syntax is exactly what it should be. (although it looks ugly)
<!DOCTYPE html>
<html>
<title>W3.CSS Template</title>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway"><style>body,h1 {font-family: "Raleway", sans-serif}body, html {height: 100%}.bgimg { background-image: url('https://www.lootjeapp.nl/forestbridge.jpg'); min-height: 100%; background-position: center; background-size: cover;
}</style><body>
<div class="bgimg w3-display-container w3-animate-opacity w3-text-white"> <div class="w3-display-topleft w3-padding-large w3-xlarge">
Logo
</div>
<div class="w3-display-middle"> <h1 class="w3-jumbo w3-animate-top">LOOTJE</h1> <hr class="w3-border-grey" style="margin:auto;width:40%"> <p class="w3-large w3-center">35 days left</p> </div> <div class="w3-display-bottomleft w3-padding-large"> Powered by <a href="https://www.w3schools.com/w3css/default.asp" target="_blank">w3.css</a> </div></div></body></html>
It shows up in the right folder and the code is fine.
HOWEVER when opening this new .html file in a browser the page is not loaded correctly. Obviously, some style element is mixed up. check it here https://www.lootjeapp.nl/Lootje/producedAndSavedByPHPAndSomethingWrong.html
To check its .html, I displayed the Source Code from the browser and copied it to clipboard Then I created a new empty file and pasted the code there . Opening that .html file in a browser displays the website without any problems (10)
So .PHP seems to generate very capable HTML code but there is something keeping it from working via the file that was created by fopen(). It is only after copying the source code to a new .html that it works flawlessly.
Is there anyone who can think of a reason why the originally .html created would not work?
(It concerns this site: https://www.lootjeapp.nl/Lootje/producedAndSavedByPHPAndSomethingWrong.html)
source https://stackoverflow.com/questions/68971898/why-does-my-php-produced-html-work-only-after-being-pasted-into-another-html
Comments
Post a Comment