2016-11-21 81 views
0

我有3列(NoteID,接下来NoteName,注)JQuery的 - AJAX - 如何获得数据,并赋以一个DIV

我基本上需要从我的名为connectionDetails php文件得到NoteID表包含查询在里面。

我目前有:

<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' style='width: 100%; border-right: 5px solid #00AA88; height: 50px;' id='output'>" . $noteName['NoteName'] . "</div>"; 
     } 

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



    </div> 
</div> 

每个这些也需要NoteID,我需要AJAX,因为当我对这些div的一个点击<div id="right-box">将显示从表中的“备注”字段中的文本不刷新页面。

请参阅my site到明白我的意思

什么是分别分配“NoteID”列随附的每一行的最好方式。

这里是connectionDetails.php脚本:

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

//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"; 
$showNoteInfo = "SELECT Note FROM Notes WHERE NoteID = '" . isset($_POST['noteid']) . "'"; 

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

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)); 
} 

?> 
+0

请说明您的具体问题或添加额外的细节,突显正是你需要的。正如目前所写,很难确切地说出你在问什么。 –

+0

我道歉它很难解释,在你看到它上面的PHP通过的笔记,我有名字的名单拉,但每种这些名称也有一个ID,但林不知道如何通过ID拉动以及名称基本上 –

回答

1

NoteID到DIV的data-nodeid属性。然后当你点击它,你可以使用$(this).data("noteid")来获取ID,并在AJAX调用发送。

此外,因为您要创建在每一行重复的ID,你不能使用'ID =“输出”在这里。你应该使用一个类而不是ID。

所以PHP的样子:

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>"; 
    } 

和JavaScript将是这样的:

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

此查询在你的脚本是错误的:

$showNoteInfo = "SELECT Note FROM Notes WHERE NoteID = '" . isset($_POST['noteid']) . "'"; 

isset()只是返回TRUEFALSE,而不是VA的值可变结构。你应该换一个if围绕整个代码:

if (isset($_POST['noteid']) { 
    $showNoteInfo = "SELECT Note FROM Notes WHERE NoteID = '" . $_POST['noteid'] . "'"; 
    $showNotes = sqlsrv_query($conn, $showNoteInfo); 
    if ($showNotes) { 
     $row = sqlsrv_fetch_array($showNotes, SQLSRV_FETCH_ASSOC); 
     echo $row['Note']; 
    } else { 
     die (sqlsrv_errors()); 
    } 
} 

你永远回荡在你的服务器脚本的查询结果。

+0

我的整个脚本http://pastebin.com/r4r1C4Yw –

+0

不仅弄来查询错,但你永远不打印查询结果,如果它是成功的。所有的脚本都会检查错误。 – Barmar

+0

好吧,看看 –