karlipoppins karlipoppins - 6 months ago 9
PHP Question

What's the best way to include html through an ajax echo command?

I am currently building a small website where the content of the main div is being filled through an Ajax call. I basically have a php script that returns the content like this:

(simplified php script...)


if(isset($_POST["id_tuto"])){
PrintHtml($_POST["id_tuto"]);
}

function PrintHtml($id)
{
switch($id)
{
case [...]:
echo "THIS IS MY HTML CONTENT";
break;
[...]
}
}



The web page then gets the text from that echo command and replaces the inner html of the content div.

My question is this : What is the best way to echo that html content? there is a lot of content each time since it's a step by step tutorial. Each string will be about 50-80 lines of HTML code. Is there a way to put that html content in a separate html file and echo that file or...?

Thanks a lot!!

Answer

You could do it like so:

<?php
    function PrintHtml($id) {
      switch($id) {
          case [...]:
?>
      <h1>Tut page 1</h1>
      <p>this is html content.</p>
<?php
      break;
      [...]
    }
  }
?>

Or perhaps:

<?php
  function PrintHtml($id) {
     switch($id) {
      case [...]:
      include 'section1.php';
      break;
      [...]
    }
  }
?>
Comments