How to achieve an effect similar to

require `content.php`; 

using AJAX? This task is relevant, for example, when creating tabs.

In content.php is, for the most part, the html-code, well, maybe with a little addition of php-code. Of course, you can stuff all HTML code in echo (in all the AJAX examples that I saw, this was done), but then the hue of the HTML syntax is lost, which is unacceptable.

Closed due to the fact that the essence of the question is not clear by the participants MasterAlex, user194374 , fori1ton , Denis Bubnov , rjhdby Jan 22 '17 at 8:24 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    Maybe stop using HTML code in .php files and switch to templating? Start with Twig - Vyacheslav Potseluyko
  • Thank you for the advice, but still? - Bokov Gleb
  • and what is wrong with downloading such a file via ajax? - Grundy
  • one
    Why do you need echo? What prevents you from displaying markup as usual? - Pavel Mayorov
  • Well, please show me an example of the output of markup via AJAX, which is in the PHP file. - Bokov Gleb

2 answers 2

To get started is to divide the entire project into 2 parts.
1 - Server part (backend)
2 - Client part (frontend)

If the user has already opened the page, the server part has already completed its work and now only the client part can work further. Nothing prevents the client side from accessing the backend using AJAX.
From the question it is not entirely clear what result is to be achieved. I suspect that there is a php file that should return an HTML page, which, in turn, the client will receive via AJAX.

For syntax highlighting with PHP code inserts, it is not necessary to store all HTML in variables or lines, it is better to use inserts or a template engine. Below is a small example of working with templates without a template engine.

 <?php $title = 'Заголовок страницы'; $body = 'Тело страницы'; $list = [1=>'Первый',2=>'Второй',3=>'Третий']; ?> <html> <body> <h1><?=$title?></h1> <p><?=$body?></p> <select name="select"> <?php foreach ($list as $value=>$title):?> <option value="<?=$value?>"><?=$title?></option> <?php endforeach;?> </select> </body> </html> 

  • Thank you for your reply. I asked this question, performing the creation of tabs, when, when switching to another tab, its contents had to be loaded via AJAX. I had only one elementary example, where the simple contents of the tabs were stored in the arguments of the 'echo' php-file. Naturally, this method is not suitable for practice - at least syntax highlighting, which is not in the arguments of functions, is needed. - Bokov Gleb

Found a solution. The load() method of the jQuery library (and without it, using AJAX is rather cumbersome) allows you to load the contents of the file inside the specified tag.

 $('#container').load('content/content.php'); 

You can read more here .