简体   繁体   English

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

[英]Pass PHP variable using jQuery html() function to div

I am trying to get sections of collage from a MySQL database using PHP and jQuery. 我试图使用PHP和jQuery从MySQL数据库中获取拼贴的部分。

I am getting data normal, but I get an error when I use html() to add the result to a div 我正在获取数据正常,但是当我使用html()将结果添加到div时,我收到错误

Here is my code: 这是我的代码:

 $("#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);
     }
 });

Does anyone have any help or ideas? 有没有人有任何帮助或想法?

As @Swellar says, wrap the $sections_checkbox variable in quotes. 正如@Swellar所说,将$sections_checkbox变量包装在引号中。 At the moment, javascript sees it as a variable instead of a string which is what you want. 目前,javascript将其视为变量,而不是您想要的字符串。 As a tip, always work with your console in the browser. 作为提示,请始终在浏览器中使用控制台。 You'll find answers to most syntax errors. 您将找到大多数语法错误的答案。 To fix your error, do this instead. 要修复错误,请执行此操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM