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-->
if($mode == 'modale')is even resulting in true?.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.<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.