【问题标题】:I need to retrieve and display a SQL PDO query done via jquery ajax我需要检索并显示通过 jquery ajax 完成的 SQL PDO 查询
【发布时间】:2021-09-01 12:54:02
【问题描述】:

我需要使用动态 HTML 填充模式(具有 .modal-sondaggio 类的模式),但我无法从通过 ajax 完成的查询中检索任何值。这是 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);
                    
                }
                
            })
        })

这是 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 };  }?>

我只是没有得到任何结果。

我在 phpMyAdmin 的 SQL 选项卡中使用静态值测试了查询,它运行良好。

这里有什么问题?

谢谢大家。

编辑:

这是div的HTML代码

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

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

<!--FINE MODAL-->

【问题讨论】:

  • 得到“没有结果”?你需要详细说明。你调试过你的脚本吗?您是否打开了错误报告?你有没有检查过if($mode == 'modale') 是否是真的?
  • 哦,对不起。你说得对。无论如何,我检查了我是否几乎到达查询并且确实如此。我已经完成了 $id_utente 的控制台日志,它给了我正确的值。但是当我尝试填充模态时,它什么也不做。如果尝试控制台记录 $sondaggio_set 的 var_dump 它会给我这个。 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" }
  • 好像 ajax 无法读取查询或结果
  • ajax 只是得到一个响应(并期望它是您的设置的 html),并将其放入类为 .modal-sondaggio 的元素中。您可以使用浏览器的 devtools 和“网络”选项卡查看返回的内容。从那里开始,如果没有填充模式,您可能引用了错误的类或元素,或者查看了错误的位置。这是我们的猜测,因为您的问题中没有提供页面 html。
  • 我现在将发布 html 页面,对我来说没问题。无论如何,我尝试回显一个像 &lt;h1&gt;Ciao&lt;/h1&gt; 这样的纯字符串,它起作用了。相反,当我尝试回显插入动态 $tupla['testo_domanda] 的 html 标记时,它不起作用。

标签: php html jquery mysql ajax


【解决方案1】:

$sondaggioSet-&gt;fetchAll(); 你在这里做错了,因为fetchAll 将返回一个数组。所以你需要将它存储在一个变量中并在foreach中使用它。

【讨论】:

  • 谢谢。有用。我做了一千次,但我不知道为什么我忘了做这一次。谢谢!
猜你喜欢
  • 2019-06-04
  • 2015-10-29
  • 2017-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-15
  • 2014-01-28
  • 1970-01-01
相关资源
最近更新 更多