javascript - 使用 jQuery html() 函数将 PHP 变量传递给 div

标签 javascript php jquery html

我正在尝试使用 PHP 和 jQuery 从 MySQL 数据库获取拼贴的各个部分。

我得到的数据正常,但当我使用 html() 将结果添加到 div

时出现错误

这是我的代码:

 $("#collage_id").on('change',function(e){
     $("#sections").fadeOut(1200);

     if(this.value==0){
         $("#sections").fadeOut(1200);
     }
     else {
         <?PHP 
             $collage_sections=new DataBase();
             $sections=$collage_sections->get_sections();
             $sections_checkbox="";
             while($section=mysqli_fetch_array($sections)){
                 $sections_checkbox.="<label class='checkbox-inline'><input type='checkbox' value='$section[0]'>$section[1]</label>";
             }
         ?>
         $("#sections").html(<?PHP echo $sections_checkbox; ?>).fadeIn(1200);
     }
 });

有人有任何帮助或想法吗?

最佳答案

正如@Swellar所说,将$sections_checkbox变量用引号引起来。目前,javascript 将其视为变量而不是您想要的字符串。提示:请始终在浏览器中使用控制台。您将找到大多数语法错误的答案。要修复您的错误,请执行此操作。

$("#sections").html("<?php echo $sections_checkbox; ?>").fadeIn(1200);

关于javascript - 使用 jQuery html() 函数将 PHP 变量传递给 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48518270/

相关文章:

javascript - jquery Accordion 帮助

javascript - 如何在 Linux 中安装 node-gyp?

JavaScript - 获取用户输入并在计算中使用以验证值

php - 努力接收 Paypal 沙盒 IPN 发布数据

php - Zend Framework 1.12 选择元素 - 名称数组问题

javascript - 选择单选按钮时隐藏/显示 div

javascript - jQuery - 如何使用元素的 HTML 获取所有样式/css(在内部/外部文档中定义)

javascript - ExtJs 启用/禁用字段

javascript - 如何使用本地存储为单个键创建多个值

php - 解决 PHP 中的日期时间差异