0

I need to populate a modal (the one with the .modal-sondaggio class) with dynamic HTML, but I cannot retrieve any values from the query I've done via ajax. This is the jQuery:

$(".mostraSondaggio").on("click", function(){
            
            var id_utente = $(this).attr("data-id_utente");
            console.log(id_utente);
                
            $.ajax({
                
                url: 'cms_ajax.php?mode=modale',
                type: 'post',
                cache: false,
                data: {id_utente: id_utente},
                dataType: 'html',
                success: function(response){
                    
                    
                    $(".modal-sondaggio").html(response);
                    
                }
                
            })
        })

And this is the PHP - SQL:

if($mode == 'modale'){
    
    

if(isset($_POST['id_utente'])){
    $id_utente = $_POST['id_utente'];

}
    
      try {
          
    
        $sondaggioSet = $connessione->prepare( "SELECT risposte.chiave_domanda, risposte.utente, domande.testo_domanda, risposte.risposta FROM (risposte INNER JOIN domande ON risposte.chiave_domanda = domande.id_domanda) INNER JOIN utenti ON risposte.utente = :id_utente" );
        $sondaggioSet->bindParam( ':id_utente', $id_utente, PDO::PARAM_INT );
        $sondaggioSet->execute();
        $sondaggioSet->fetchAll();
          
        } catch ( PDOException $e ) {
        echo $e->getMessage();
        die();
      }
    
    
    foreach ($sondaggioSet as $tupla){?>
        <h1>Ciao bello</h1>
        <h3><?php echo $tupla['testo_domanda']?></h3>
        <h4><?php echo $tupla['risposta']?></h4>
        
    <?php };  }?>

I'm just getting no result.

I tested the query in the SQL tab in phpMyAdmin with a static value and it works well.

What's the problem here?

Thank you all.

EDIT:

This is the HTML code of the div

  <!--qui va la MODAL per mostrare il sondaggio-->

<div class="modal-sondaggio">
</div>

<!--FINE MODAL-->
5
  • 1
    Getting "no result" ? You need to elaborate. Have you debugged your script? Do you have error reporting turned on? Have you even checked to see if if($mode == 'modale') is even resulting in true? Commented Jun 16, 2021 at 17:08
  • Oh, sorry. You're right. Anyway, I checked if I almost reach the query and it does. I've done a console log of the $id_utente, and it gives me the correct value. But when I try to populate the modal it simply does nothing. If if try to console log the var_dump of the $sondaggio_set it gives me this. object(PDOStatement)#2 (1) { ["queryString"]=> string(222) "SELECT risposte.chiave_domanda, risposte.utente, domande.testo_domanda, risposte.risposta FROM (risposte INNER JOIN domande ON risposte.chiave_domanda = domande.id_domanda) INNER JOIN utenti ON risposte.utente = :id_utente" } Commented Jun 16, 2021 at 17:15
  • seems like ajax can't read the query or the result Commented Jun 16, 2021 at 17:17
  • 1
    The ajax just gets a response (and expecting it to be html by your settings), and puts it into an element with class of .modal-sondaggio. You can see what is being returned using your browsers devtools and 'network' tab. From there, if the modal is not populating, you may be referencing the wrong class or element, or looking in the wrong place. Its guesswork from our end as no page html was provided in your question. Commented Jun 16, 2021 at 17:33
  • I now will post the the html page, no problem for me. Anyway, I tried to echo a pure string like this <h1>Ciao</h1> and it worked. Instead, when I try to echo an html tag interpolated with the dynamic $tupla['testo_domanda] it doesn't work. Commented Jun 16, 2021 at 17:38

1 Answer 1

3

$sondaggioSet->fetchAll(); you are doing it wrong here since fetchAll will return an array. So you need to store it in a variable and use that in foreach.

Sign up to request clarification or add additional context in comments.

1 Comment

THANK YOU. IT WORKS. I did it like a thousand times, but I don't know why I forgot to do this time. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.