2016-12-14 1 views
0

Mon script PHP, que j'appelle via XMLHttpRequest, exécute une requête et vérifie une condition, puis affiche un extrait HTML avec du code Smarty. Lorsque j'essaie d'insérer ce code dans une balise <div>, en appelant la fonction jQuery.html() ou en définissant la propriété innerHTML, le code Smarty est imprimé tel quel, et n'est donc pas interprété comme du code Smarty. Comment pourrais-je résoudre ce problème?Obtenir des données au format Smarty à partir du script PHP

Code pertinent:

PHP Script:

<?php 
     $mysqli = new mysqli("localhost", "<<<SQL USERNAME>>>", "<<<SQL PASSWORD>>>", "<<<SQL DATABASE>>>"); 
     $id = $_GET["idmf"]; 
     $cat = $mysqli->query("<<<SQL QUERY>>>")->fetch_assoc()["category"]; 
     if ($cat == $_GET["cat"]) 
      echo 'blahblah'; 
?> 

fichier TPL:

{foreach from=$manufacturers item=manufacturer name=manufacturers} 
    <div id="mffilter_{$manufacturer.id_manufacturer}"></div> 
    <script> 

     var xhttpf; 
     if (window.XMLHttpRequest) 
     { 
      xhttpf = new XMLHttpRequest(); 
     } 
     else 
     { 
      // code for IE6, IE5 
      xhttpf = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xhttpf.onreadystatechange = function() 
     { 
      if (this.readyState == 4 && this.status == 200) 
      { 
       document.getElementById("mffilter_{$manufacturer.id_manufacturer}").innerHTML = this.responseText; 
      } 
     }; 
     xhttpf.open("GET", "<<<ENDPOINT>>>.php?cat=food&idmf={$manufacturer.id_manufacturer}", true); 
     xhttpf.send(); 

    </script> 
+0

Smarty doesn Ne travaille pas comme ça. Vous devez analyser le html désiré dans le script php avec smarty avant de faire écho. De même, si vous utilisez Prestashop, utilisez ses contrôleurs et ses méthodes d'accès db pour les appels ajax. – TheDrot

+0

@TheDrot Pourriez-vous m'adresser d'une manière ou d'une autre? – jinzo78

Répondre

0

Essayez de remplacer votre code de fichier TPL avec le code suivant:

{foreach from=$manufacturers item=manufacturer name=manufacturers} 
    <div id="mffilter_"{$manufacturer.id_manufacturer}></div> 
    <script> 

     var xhttpf; 
     if (window.XMLHttpRequest) 
     { 
      xhttpf = new XMLHttpRequest(); 
     } 
     else 
     { 
      // code for IE6, IE5 
      xhttpf = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xhttpf.onreadystatechange = function() 
     { 
      if (this.readyState == 4 && this.status == 200) 
      { 
       document.getElementById("mffilter_"{$manufacturer.id_manufacturer}).innerHTML = this.responseText; 
      } 
     }; 
     xhttpf.open("GET", "<<<ENDPOINT>>>.php?cat=food&idmf="{$manufacturer.id_manufacturer}, true); 
     xhttpf.send(); 

    </script>