T
T
Tema_TM2018-06-10 18:24:53
PHP
Tema_TM, 2018-06-10 18:24:53

How to generate an html page and give the client only a fragment of the page?

Good day dear programmers. I can't figure out how to get only a part of the html from the server and not the whole page in order to update the innerHTML later. You most likely will not be interested in reading the spoller from the bottom. I just need to generate a piece of html code for the client, but in the end I get a full page with DOCTYPOM (

first I redirect .htaccess to the public/index.php directory

AddDefaultCharset utf-8
RewriteEngine On
RewriteRule .* public/$1


in the public directory itself

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php


As a result, I can only access files in public, but not app

in the controllers/route.php directory, I connect files depending on the url for example news_controller.php which then generates a template

on the client

(function()
{
  function idN(idN)
  {
    return document.getElementById(idN);
  }

  function ajax(method, request)
  {
    var xhr = new XMLHttpRequest();

    xhr.open(method, request, true);

    xhr.onreadystatechange = function()
    {
      if(xhr.readyState === 4)
      {
        if(xhr.status === 200)
        {
          console.log(xhr.responseText);

          for(var n = 0; document.querySelectorAll('ul li a').length > n; n++)
          {
            document.querySelectorAll('ul li a')[n].onclick = function()
            {
              spa(this.href);

              ajax('POST', '/app/controllers/route.php?controller=' + this.href);

              return false;
            }
          }
        }

        else console.log(xhr.status + ' ' + xhr.statusText);
      }
    }

    xhr.send(null);
  }

  function spa(links)
  {
    history.pushState(null, null, links);
  }

  window.onpopstate = function()
  {
    ajax('POST', '/app/controllers/route.php?controller=' + location.href);
  }

  ajax('POST', '/app/controllers/route.php?controller=' + location.href);
}());


<?php

  $views = 
  [
    'header',
    'profile_header',
    'content',
    'footer'
  ];<code lang="php">

<code lang="php">
<?php

  $controller = clean_str($_SERVER['REQUEST_URI']);
  $controller = explode('/', rtrim($controller, '/'));

  function get_controller($controller)
  {
    if(empty($controller[0]) && empty($controller[1]))
    {
      if(logedin()) $controller = 'news';
      else $controller = 'login';
    }

    else if(empty($controller[0]) && !empty($controller[1]) && empty($controller[2]))
    {
      if(logedin())
      {
        if($controller[1] === 'id'.user_id()) $controller = 'id';
        else $controller = $controller[1];
      }

      else $controller = 'login';
    }

    else
    {
      header("HTTP/1.0 404 Not Found");
      exit;
    }

    return $controller;
  }

  $controller = get_controller($controller);

  require_once $controller . '_controller.php';
</code>

У меня есть такая структура:

app
--config
---config.php
--model
---model.php
--views
---ui
----header.php
----footer.php
---index.php
--controllers
---route.php
---news_controller.php
--helper
---helper.php
--bootstrap.php
public
--css
--js
--fonts
--icons
--images
--index.php
--.htaccess
.htaccess

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
ThunderCat, 2018-06-10
@ThunderCat

it’s not entirely clear what’s going on with routes and views, in a normal system a piece of html is simply made as needed, and in the controller the logic chooses what to render - the entire template or just a piece from the desired file, and the view already renders what is needed - the entire page with a header footers and so on, or just a file. Since I don’t see how it all works for you, I described the general principle.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question