javascript – Insert PHP in div with Jquery (.html)


I have a div that will receive the content, I have a vertical menu that will define the content that so-and-so will see. The problem is that it does the .load() of the file, but it loads as far as it appears

I tried using .html() , and putting a .php include , it does, but it gives the same problem. Does not read information after PHP. I tried to write the page inside the .html() , but it doesn't work either, I made the double quote corrections with a backslash (\") and it didn't work.

In the official libraries I couldn't get an answer if the case is with Database and PHP. but I can't understand why it doesn't work.

My code is a little big, as there are several links and the contents are extensive, I made a reduced example, because I think the problem is one of concept, not syntax.

  • The problem is not in PHP and MySQL, because if I open the page only, outside of jQuery, it works fine, it takes all the data.

  • The jQuery database and connection to MySQL are already being made and they work, I didn't put it in order not to fill it with useless code.


 <script type="text/javascript"> $(document).ready(function() { $("#link1").click(function() { $("#conteudo").html("Aqui vai o conteúdo do Link 1, via HTML mesmo, pois não tem PHP.. Esse aqui funciona normalmente!"); }); $("#link2").click(function() { $("#conteudo").load('link2.php'); }); }); </script>


 <a href="#link1" id="link1">Link 1</a> <a href="#link2" id="link2">Link 2</a> <div id="conteudo">Este conteúdo é para ser alterado, conforme o usuário vá clicando no link... O link 1 funciona normal, pois não tem php, o link 2 funciona até aparecer o PHP...</div>

PHP (link2.php)

 <?php $query = @mysql_query("SELECT * FROM dados;"); while($linha = @mysql_fetch_array($query)) { ?> <p><?php echo $linha['nome']. " - ". $linha['sobrenome']; ?> <a href="imagens/foto.jpg">Veja a foto</a> </p> <?php } ?>


TL;DR – Include the php files that define the connection in link2.php . jQuery's .load .load() will only work when you can make a direct request to link2.php and receive the desired html fragment.

You are confusing two things. Interpretation of server-side PHP commands ( include ) with client-side ajax functionality.

jQuery's load method expects link2.php return a page or html fragment. In the case link2.php alone does not produce anything, it just fails silently ( @ ) due to the lack of an active connection.

Request 1 -> Pagina 1 -> Request 2 -> link2.php (sem conexão) = Falha silenciosa

What you need to do is create a link2.php page that works by itself (ie returns results when a request is made directly from the browser). For that you must include code to connect to the database and everything that is necessary to print the results.

Note that this functionality is quite different from a require or include ; these do everything server-side:

Request 1 -> 
    Pagina 1, cria conexões etc
        include 'link2.php'
            usa conexão "definida" na página 1
<- Servidor interpreta o conjunto e navegador recebe uma resposta HTML unificada

In this case there is no second request , the content of link2.php is interpreted together with the current page content by the server as if it were just another piece of PHP code in the body of the page. The browser is not aware that these are two separate files.

Scroll to Top