0
votes

I have added a review section to my website. It works perfectly but the w3 validator shows errors about stray tags (Doctype, Html, Head, body...)

The review section generates contents and consists of php, css and js files. I have put all files into the folder "recensioni" and I have included the index.php path inside the body of my homepage. recensioni/index.php includes in turns php files with html tags. These tags become duplicated in the main html and affects its flow: "Error: Stray doctype". ... "Start tag body seen but an element of the same type was already open" "End tag for body seen, but there were unclosed elements"

Homepage index.php:

<!DOCTYPE html>
...
<p id="recensioni" style="height:10px;"></p>
<div class="group-container txt-center" >
<h1 >CHI HA FREQUENTATO DICE</h1>
<div  class= "back-corsi">
 <?php include_once $_SERVER['DOCUMENT_ROOT']. '/recensioni/index.php';?>
</div>
</div>
...

recensioni/index.php:

...
include 'recensioni.html.php';

try {
      $sql='SELECT * FROM corsi';
      $result=$pdo->query($sql);
    } 
catch (Exception $e) {
      $error='Errore nel recupero recensioni: ' . $e->getMessage();
      header('Location: .?errorDB=218');  
      exit(); 
    }
foreach ($result as $row) {
    $corsi[]=array ('id'=>$row['id'], 'corso'=>$row['nome_corso']);   
    }

 include 'nuova_recensione.html.php';

nuova_recensione.html.php:

<!DOCTYPE html>
...
<body onload="<?php if($location==="inputField"){ ?> location.href='#inputField'; <?php }  if (isset($action)){ ?> alert_message() <?php  } ?>">

    <?php
    if (isset($action)){?>
    <div id="dialog-message" title="<?php echo htmlout($title);?>">
    <p id="message"><?php echo htmlout($message) ?></p>
    </div>
    <?php  } ?>

<div class="row row-one-colunm-padding"> 
...

How can I solve? What solution should I adopt? Thanks

2
nuova_recensione.html.php is a PHP file, so you can omit <!DOCTYPE html> from that file. This will also avoid pulling in two of those tags, and another set of <head>, <body> etc.. Also, there's no need for .html.php, you can simply name it .php as the extension. The file type is strictly for you to know what it contains by just looking at it. - Brxxn
As you can see in the code I have posted, I need the body tag to use the onload event. Then, if I remove <!DOCTYPE html> and other tags from php file, where do I put <link>stylesheet? (.html.php is to reconignize from pure php. Is a choise, I know it's not a constrain) - Giovanna Virdis
You can keep the <link>'s and everything else where they are, you can remove <!DOCTYPE html> without issues. - Brxxn
I have tried, but removing <!DOCTYPE html> I still have other errors about head and body - Giovanna Virdis

2 Answers

0
votes

you have differents way (look for on documentation) but you can test require("file.php"); require "file.php"; require_once("file.php") or the include type

0
votes

I suggest using an inc/ folder to stash your header.php and footer.php files. The header file should include all code at the beginning of an HTML file down to the opening body tag (and even lower if you want to include a global navigation system). The footer file should include all code from the closing body tag down (you can place your footer content here if you choose).

You can then include these files as the first and final line of a .php file using either the include() or require() function, depending on your needs.

This strategy has worked well for me with projects that I have created! The only other suggest that I would give would be to use a template engine with routing configuration that should mitigate your issue.