2012-08-02 68 views
0

當點擊鏈接時,鏈接應該運行從相同頁面上的數據庫中獲取信息的PHP代碼。使用ajax通過文本鏈接在同一頁面上返回php代碼

我已經實現了使用AJAX,一個窗體和一個按鈕,但真的希望使用鏈接而不是按鈕。

我已經設法讓鏈接工作,但PHP腳本創建的數據庫中的表格僅在刷新瀏覽器時閃爍一秒鐘。

如何停止刷新以便顯示錶格?

我會很感激任何幫助。

我的代碼是:

<script language="javascript" type="text/javascript"> 

    function myFunction(){ 
    var ajaxRequest; 

try{ 
    // Opera 8.0+, Firefox, Safari 
    ajaxRequest = new XMLHttpRequest(); 
} catch (e){ 
    // Internet Explorer Browsers 
    try{ 
     ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
    } catch (e) { 
     try{ 
      ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
     } catch (e){ 
      // Something went wrong 
      alert("Your browser has Failed"); 
      return false; 
     } 
    } 
} 

ajaxRequest.onreadystatechange = function(){ 
    if(ajaxRequest.readyState == 4){ 
     var ajaxDisplay = document.getElementById('ajaxDiv1'); 
     ajaxDisplay.innerHTML = ajaxRequest.responseText; 
    } 
} 

    document.proffForm.submit(); 
ajaxRequest.open("GET", "proff/all_proff.php"); 
ajaxRequest.send(null); 


    } 

    </script> 

    <form name='proffForm' > 
    <a href="#" onclick='myFunction()' >List All Professions</a> 
    </form> 

    <div id='ajaxDiv1'></div></p> 

PHP

 <?php 
    include '../dbc.php'; 



    $allprof1 = mysql_query("SELECT * 
        FROM users 
        ORDER BY profession"); 

    $qry_result = mysql_query($allprof1) or die(mysql_error()); 
    ?> 
    <?php  
    $display_string = "<table border='1' cellpadding='5' width='100%'    bordercolor='000099'border='solid'>"; 
    $display_string .= "<tr align='left' bgcolor='009966'>"; 
    $display_string .= "<th><span class='titlehdr3'>Profession</span></th>"; 
    $display_string .= "<th><span class='titlehdr3'>Business Name</span></th>"; 
    $display_string .= "<th><span class='titlehdr3'>Profile</span></th>"; 
    $display_string .= "</tr>"; 


    while($row = mysql_fetch_array($qry_result)){ 
$display_string .= "<tr>"; 
$display_string .= "<td><span class='tabs'>$row[profession]</span></td>"; 
$display_string .= "<td><span class='tabs'>$row[full_name]</span></td>"; 
$display_string .= "<td><span class='tabs'>$row[url]</span></td>"; 
$display_string .= "</tr>"; 

    } 

    $display_string .= "</table>"; 
    echo $display_string; 

    ?> 

回答

0

我所看到的,你所添加的形式提交功能。在阿賈克斯請求我們不需要任何提交行動,所以刪除行

document.proffForm.submit(); 

從您的代碼。它會完美地工作。

+0

非常感謝你的快速回答它的作品,感覺像這樣一個白癡!我敢肯定,我嘗試過! – 2012-08-02 10:38:29

+0

你總是歡迎。 – 2012-08-02 10:45:50

相關問題