dongwei4096 2014-09-23 09:49
浏览 44

使用php中的唯一div Id传递给javascript并用于运行另一个php脚本

I have a div that uses some unique id from a mysql db that I have to pass to my javascript file so I could display results from another php script in a particular div with a specific id that corresponds to the one obtained from the db.

index page.

<div class="wrapper">
<?php 
    $query = mysqli_query($con, 'SELECT * FROM in_table limit 10 ');

    while($result = mysqli_fetch_array($query)){ ?>
        <img src= <?php echo $result['user_image_url']; ?> />

    <?php 
        echo $result['msg'].'</br>';
        echo '<a href="#'.$result['statusID'].'">More...</a></br>';

        echo '<div id="'.$result['statusID'].'" class="rest" style="width: 400px; height: 100px;"></div>';
    }
?>

The div is inside the while loop.

ajax.js

$('a').on('click', function(){
    var hash = this.hash.replace('#','');

    $.post('content.php', {id: hash}, function(data){
        $('div#?????').html(data);
    });

})

So how do I call that div inside?

content.php

<?php
    include('ajax.php');

    if (isset($_POST['id'])) {
        $id = $_POST['id'];
    }

    $query = mysqli_query($con, "SELECT * FROM comments where statusID = '".$id."'");

    while($row = mysqli_fetch_array($query)){
        echo $row['message'].'</br>';
    }   
?>
  • 写回答

2条回答 默认 最新

  • doumei1926 2014-09-23 09:53
    关注

    Why don't you use the hash in the selector?

    $.post('content.php', {id: hash}, function(data){
        $('div#' + hash).html(data);
    });
    

    I also heard its more lightweight and more intentionally correct to use GET when retrieving something and using POST when modifying something.


    Edit:

    You said you were using all integers as IDs, which is bad for DOM. Try putting some text before the IDs like so:

    echo '<a href="#t'.$result['statusID'].'">More...</a></br>';
    echo '<div id="t'.$result['statusID'].'" class="rest" style="width: 400px; height: 100px;"></div>';
    

    Now the ID will be something like t1000 and NOT 1000.

    Then in your JS do

    $.post('content.php', {id: hash}, function(data){
        $('div#t' + hash).html(data);
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数