2012-06-24 12 views
0

我在PHP頁面中使用AJAX從MySQL數據庫調用聯繫人列表。數據被正確調用,但是當我在瀏覽器中調用PHP頁面時,我可以在元數據之前看到聯繫人列表(來自數據庫的數據)顯示在頁面的頂部。該列表應該顯示在定義的DIV中,但由於某種原因它不是。下面是我的代碼:在AJAX中顯示來自數據庫的數據的錯誤地方

Ajax代碼

function getUserList(){ 

var ajaxDisplay = myform.getElementById('displayList'); // HTML Div where data should be displayed 

try{ //for Firefox, Chrome, Opera, Safari, IE7+ 

    var activeX = new XMLHttpRequest(); //initializing object 
}catch(e){ 

    try{// for IE5 , IE6 
    var activeX = new ActiveXObject(Msxml2.XMLHTTP); 
    } 
    catch(e){ 

    alert("Dude your browser is in Jurassic era!"); 
    } 
} 

    activeX.readystatechange = function{ 

    if(activeX.readyState == 4){ 
    var queryResult = activeX.responseText; 

    if(!queryResult){ 
    ajaxDisplay.innerHTML = 'Error in populating list'; 
    }else{ 
    ajaxDisplay.innerHTML = queryResult; 
    } 

    } 
} 

    activeX.open("GET", "?action=contactsandgroups", true); 
    activeX.send(null); 
} 

PHP/HTML代碼

<div class="contacts_list_data_contacts" id="displayList"></div> 

MySQL函數

while($row=mysql_fetch_assoc($res_info)){ 

print '<div class="contacts_headings_01"> 
<div class="contacts_checkbox_01"><input name="contact_id[]" id="contact_id" type="checkbox" value="'.$row['contact_id'].'" style="margin-top:0px ; margin-left:-3px;" /></div> 
<div class="contacts_firstname_01_contacts">'.$row['firstname'].'</div> 
<div class="contacts_firstname_01">'.$row['lastname'].'</div> 
<div class="contacts_firstname_01">'.$row['company'].'</div> 
</div>'; 
} 

當我查看頁面源時,它顯示它在頁面頂部打印數據。這發生在Firefox,Chrome和Internet Explorer中。

回答

2

我看到的是,你想要?action=contactsandgroups這個輸出被填充在div#displayList裏面。如果是這種情況,請嘗試以下方法:

<head>部分補充一點:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
<script type="text/javascript"> 
    function getUserList() 
    { 
     ajaxDisplay = $("#displayList"); 
     ajaxDisplay.load('?action=contactsandgroups'); 
    } 
</script> 

試試吧,看看它是否工作。如果沒有,請將HTML代碼發送給我,並在其中調用該函數。

+0

感謝您的回答,我想我需要先對我的代碼進行一些更改。 – alkhader

相關問題