简体   繁体   中英

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.

I am getting data normal, but I get an error when I use html() to add the result to a 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. At the moment, javascript sees it as a variable instead of a string which is what you want. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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