Load a part of page in symfony 3 directory from jquery

问题: I use Symfony and I have an element in web/getTable.php file I'd like to load. The code of this element is here: <p id="hello">Hello World!!!</p>...

问题:

I use Symfony and I have an element in web/getTable.php file I'd like to load. The code of this element is here:

<p id="hello">Hello World!!!</p>

How can I get it? Before posting this,I've tried this:

$('#btn').click(function(){
$('span').load("{{asset('getTable.php #hello')}}");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<button id="btn">load</button>
<span id="#test"></span>


回答1:

A better approach might be to load it normally in a hidden span, and then when the button is clicked, you just make the span visible. If its long loading or you don't want to preload it, you have to do an actual Ajax call (example) and build the HTML. It would of course be better to use CSS for the display:none, but this should be the basics to get it working.

Html:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js">     </script>
<button id="btn">load</button>
<span id="#test" style="display:none">{{asset('getTable.php #hello')}}</span>

JavaScript:

$('#btn').click(function(){
    $('span').show();
});

回答2:

the really content of getTable.php is this

    
   
    //this is in my twig file
    <script>
        $('btn').click(function(){
        $('#test').load("{{asset('getTable.php #list')}}");
        });
    
    
    
     </script>
    //in the getTable.php file
  <table>
    <thead>
  <tr>
    <th>Type</th>
    <th>Nombre de Place Libre</th>
    <th>Nombre de Place Exigé</th>
  </tr>
  </thead>
  <tbody>
<?php
$bdd=new PDO('mysql:host=localhost;dbname=gestion_ressources_humaines', 'root','');

$reponse=$bdd->query('SELECT id, type, nbPlaceLibre, nbPlaceExige FROM fonction');

while ($donnees = $reponse->fetch())
{
?>
  <tr>
    <td style="display:none;"><?php echo $donnees['id']; ?></td>
    <td><?php echo $donnees['type']; ?></td>
    <td><?php echo $donnees['nbPlaceLibre']; ?></td>
    <td><?php echo $donnees['nbPlaceExige']; ?></td>
  </tr>
<?php
}

?>
</tbody>
</table>
<?php
$reponse->closeCursor(); // Termine le traitement de la requête

?>
<script>
$(function(){
  $("tr").click(function(){
  $('tr').removeClass('selected');
if($(this).hasClass('selected'))
    $(this).removeClass('selected');
else
  $(this).addClass('selected');
        });
        });

</script>
    
    <p id="hello">Hello world!!!</p>
    
  • 发表于 2019-01-11 21:37
  • 阅读 ( 10 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除