dswg47377 2016-11-22 14:44
浏览 60

JQuery - AJAX - 单击Div显示与ID相关的SQL数据

I am trying to dynamically display helpful Notes on screen to users.

If you go to http://discovertheplanet.net/general_notes.php

The list on the left is how many rows there are in my Notes table.

There are three columns in my database, 'NoteID', 'NoteName' and 'Note'

Each 'NoteName' is pulled from the database and is given an ID using the 'NoteID' column in the database

The PHP/HTML:

<div class="main-container-notes">
    <div id="left-box">
        <?php 

        echo "<div style='width: 100%;'>";

        while( $noteName = sqlsrv_fetch_array( $resultNotes, SQLSRV_FETCH_ASSOC)) 
        {
            echo "<div class='hvr-bounce-to-right1 hover-cursor output' data-noteid='{$noteName['NoteID']}' style='width: 100%; border-right: 5px solid #00AA88; height: 50px;'>" . $noteName['NoteName'] . "</div>";
        }

        echo "</div>";
        ?>
    </div>
    <div id="right-box">

    <!-- Where the actual full note description will go -->

    </div>
</div>

My current script: which is in my <script type="text/javascript" src="scripts/scripts.js"></script>

Looks like:

$(".output").click(function() {
    var noteid = $(this).data("noteid");
    $("#right-box").load("connectionDetails.php", { noteid: noteid });
});

EDIT

This is my connectionDetails.php:

<?php
$myServer = "replacement";
$connectionInfo = array('Database' => 'DiscoverThePlanet', 'UID' => 'replacement', 'PWD' => 'replacement');

//connection to the database
$conn = sqlsrv_connect($myServer, $connectionInfo)
  or die("Couldn't connect to SQL Server on $myServer"); 

//Test connection to server
// if ($conn) 
// {
//     echo "connection successful";    # code...
// }

//Defining my queries
$getNotes = "SELECT NoteID, NoteName, Note FROM Notes";
$getTemplateNotes = "SELECT TemplateNoteID, TemplateNoteName, TemplateNote FROM TemplateNotes";
$getReplaceVariables = "SELECT ReplaceVariableID, ReplaceVariableName, ReplaceVariableNote FROM ReplaceVariables";

$resultNotes = sqlsrv_query( $conn, $getNotes );
$resultTemplate = sqlsrv_query($conn, $getTemplateNotes);
$resultVariables = sqlsrv_query($conn, $getReplaceVariables);

if( $resultNotes === false) 
{
    die( print_r( sqlsrv_errors(), true) );
}
if( $resultTemplate === false) 
{
    die( print_r( sqlsrv_errors(), true) );
}

if( $resultVariables === false) 
{
    die( print_r( sqlsrv_errors(), true) );
}

?>

Not sure if it affects anything but i also have this included like so

<?php include 'connectionDetails.php'; ?>

So am i on the right lines as I'm new to this, and how do I now use the ID's I have, to pull through the 'Note' column when I click on the appropriate Div?

Example: I click on 'Test note' which has a NoteID of 1 it then checks the 'Note' column for NoteID 1 and pulls through 'This is a test piece of helpful text shown in my right container on the same screen'

I hope I have explained this well enough, if you need any more info, please just ask.

Many thanks in advance!

  • 写回答

1条回答 默认 最新

  • dougou6727 2016-11-22 18:27
    关注

    i think make a ajax call is easy then your way try this :

    $(".output").click(function() {
        var noteid = $(this).data("noteid");
    
        $.get("connectionDetails.php?id="+noteid, function(data, status){
        $("#right-box").html(data);
    
    });
    
    });
    

    in you php code try $_GET['id']; to get noteid value

    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择