drjtua5953 2014-08-14 15:48
浏览 45
已采纳

单击链接时从数据库中获取结果

I'm using this HTML code;

if ($forum['type'] != 'c' && !$forum['linkto'] && $forum['posts'])
{
    $forum['collapsed_image'] = '
        <div class="expcolimage">
            <a id="forum_name" fid="'.$fid.'">
                <img src="images/collapse_collapsed.gif" id="ann_'.$forum['fid'].'_img" class="expander" alt="[-]" title="[-]" />
            </a>
        </div>';
}
else
{
    $forum['collapsed_image'] = '';
}

What I want to do is to make it so when this link is clicked then an sql query should be run on a PHP page which fetches a result from database show that result in a <div> on an HTML page (or to show that result just under that link on the same page)

Due to limited knowledge in javascript I'm unable to code a javascript function which do that process, can you please provide me an example? I'll be very thankful to you.

Thank you!

PLEASE NOTE: I only want to use javascript and not jQuery

  • 写回答

2条回答 默认 最新

  • dsstlsqt696435 2014-08-14 15:58
    关注

    This is how you can do this:

    test.php - the entire script is to be placed on this single script.

    <?php
    
    // Handle GET Request
    if (isset($_GET['loadData']) && isset($_GET['id']))
    {
        // Dummy Response
        // you should query the database here
        exit("hello #". $_GET['id']);
    }
    
    ?>
    
    <script type="text/javascript">
    
    function ajaxCall(url, callback) {
        var xmlhttp;
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 ) {
               if(xmlhttp.status == 200){
                   callback(xmlhttp.responseText);
               }
               else if(xmlhttp.status == 400) {
                  alert('There was an error 400')
               }
               else {
                   alert('something else other than 200 was returned')
               }
            }
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send();
    }
    
    function loadData(id)
    {
        ajaxCall('test.php?loadData&id='+ id, function(result) {
            document.getElementById('result').innerHTML = result;
        });
    }
    
    </script>
    
    Click any of these links: <br>
    Result: <div style="display: inline;" id="result"></div>
    
    <br><br>
    
    <?php
    
    // this is your initial database query result with links
    for ($i = 1; $i <= 3; $i++)
    {
        echo "&bull; Hello, I am #$i. <a href='#' onclick='loadData($i);'>Click here<a> to load data.<br>";
    }
    
    ?>
    

    Demo:

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大