weixin_33713503 2017-10-04 15:03 采纳率: 0%
浏览 28

PHP:在$ .ajax中返回html

I am trying to return HTML through $.ajax, this is my code:

 $(document).ready(function() {

    $(".click").on('click',function() {                
      var id= $(this).attr("data-id"); 

      $.ajax({     
        type: "POST",
        url: "getevents.php", 
        data: {id:id}, 
        dataType: "html",                  
        success: function(response){                    
            alert(response);
            $('.headimg').html(response);
        }
     });
 })}) 

My PHP code:

 $singleevent = mysqli_query($link, "SELECT `content` FROM `events` WHERE `id` 
= $id") ;
if(!$singleevent){
    echo mysqli_error();
}
else {

$result = mysqli_fetch_array($singleevent); 
 echo $result; 
}

Link to be clicked:

<a class='click' data-id='<?=$rows["id"];?>'><?=$rows['title'];?></a>

In alert() this is what I get Array

If I do print_r() instead of echo this is what I get

 [0] => <div class='row'>
  <div class='col-sm-12'>
   <img src='img/img.png' class='img-responsive'>
  </div>
  <div class='col-xs-12'>
   <p> October 14th  | All Day</p>

  </div>

</div>
    [content] => <div class='row'>
  <div class='col-sm-12'>
   <img src='img/img.png' class='img-responsive'>
  </div>
  <div class='col-xs-12'>
   <p> October 14th  | All Day</p>

  </div>

</div>

How to I put it in HTML rather than array?

  • 写回答

1条回答 默认 最新

  • weixin_33722405 2017-10-04 15:08
    关注

    The result is what you get if you do echo array(1,2,3), you can not echo a array. Besides that, a PHP array isn't a Javascript array, so javascript doesnt understand the result.

    You currently fetch the result from the database, which has two values because you fetch it as fetch_array(), which returns the data double:
    - Once as numerical values ([0],[1],[2] etc)
    - Once as named values (['content'],['example'],['etc'])

    To remedy this, use fetch_assoc(). That will only return the last one (the named).

    But, then you still have an array with a value, so javascript will still get "Array" as you try to echo that. You can now do echo $result['content']; to fix this.


    There is a (IMO) better method, which is a bit more complex, but a lot more expendable (what if you want 2 or 3 or ... values returned?), which is called JSON format.

    In PHP you do echo json_encode($result); which will result in a weird look array in text format. Now you go to you Javascript AJAX function and add dataType: "json" to it. You can now use response.content, or response.example, or response.whateverYouSelectInPHP

    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同