2010-02-11 78 views
-1

我使用AJAX在MySQL數據庫中存儲數據,它正在工作。但是我面臨一個問題來使用AJAX檢索數據。我如何從php中使用ajax獲取數據?

我的用於存儲數據(它的一個帖子評論頁)編碼:

function sendMail(form_id) { 
    // Do a client-side check; if it passes, then move on; 
    // otherwise, report an error to the user 

    //if (!validatePage(form_id)) return; 
    if(!checkMail(document.getElementById('txtEmail').value)) 
    { 
     document.getElementById('txtEmail').focus(); 
     return false; 
    } 
    window.scrollTo(0,0); 
    divId = "results"; 
    var str = ""; 
    var elem = document.getElementById(form_id).elements; 
    for (var i = 0; i < elem.length; i++) { 
     if (
      (elem[i].type == "hidden") || 
      (elem[i].type == "text") || 
      (elem[i].type == "textarea") 
     ) { 
      // Text field 
      str += elem[i].id + "=" + escape(encodeURI(trimField(elem[i].id))) + "&"; 
     } 
     else if (elem[i].type == "checkbox") { 
      // Check box 
      if (elem[i].checked) { 
       str += elem[i].id + "=on&"; 
      } 
      else { 
       str += elem[i].id + "=off&"; 
      } 
     } 
     else if (elem[i].type == "select-one") { 
      // Drop-down menu (SELECT) 
      var sel = elem[i]; 
      str += sel.id + "=" + sel.options[sel.selectedIndex].value + "&"; 
     } 
    } 
    str = str.substring(0, str.length-1); 
    str = str.replace(/%250A/g,"\n") // Make sure that line breaks get transmitted properly 
    if (form_id == "contact_form") { 
     form_page = "sendMail.php"; 
    } 
    AjaxRequest(form_page, str, "post"); 
} 

// Make the AJAX request 
function AjaxRequest(url, parameters, type) { 
    http_request = false; 
    if (window.XMLHttpRequest) { 
     // Mozilla, Safari,... 
     http_request = new XMLHttpRequest(); 
     if (http_request.overrideMimeType) { 
      // set type accordingly to anticipated content type 
      //http_request.overrideMimeType('text/xml'); 
      http_request.overrideMimeType('text/html'); 
     } 
    } 
    else if (window.ActiveXObject) { 
     // IE 
     try { 
      http_request = new ActiveXObject("Msxml2.XMLHTTP"); 
     } 
     catch (e) { 
      try { 
       http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
      catch (e) { 
      } 
     } 
    } 
    if (!http_request) { 
     alert("Cannot create XMLHTTP instance"); 
     return false; 
    } 
    if (type == "post") { 
     // POST 
     http_request.open('POST', url, true); 
     http_request.onreadystatechange = AjaxRequestCb; 
     http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
     http_request.setRequestHeader("Content-length", parameters.length); 
     http_request.setRequestHeader("Connection", "close"); 
     http_request.send(parameters); 
    } 
    else if (type == "get") { 
     // GET 
     http_request.open('GET', url + parameters, true); 
     http_request.onreadystatechange = AjaxRequestCb; 
     http_request.send(null); 
    } 
    // if...else 
} 

// AJAX Callback 
function AjaxRequestCb() { 
    if (
     http_request.readyState == 1 || 
     http_request.readyState == 2 || 
     http_request.readyState == 3 
    ) { 
    } 
    else if (http_request.readyState == 4) { 
     if (http_request.status == 200) { 
      result = http_request.responseText; 
      document.getElementById(divId).innerHTML = result; 
      // If CAPTCHA failed, then repopulate it 
      if (result.indexOf("CAPTCHA") > -1) { 
       Recaptcha.reload(); 
      } 
      else { 
       // Clear form after email has been sent 
       document.forms["contact_form"].reset(); // Not robust, I know (will modify later) 
       Recaptcha.reload(); // Display a new CAPTCHA 
      } 
     } 
     else { 
      alert("Callback failed. There was a problem with the request."); 
     } 
    } 
} 

// Remove any unneccessary whitespace 
function trimField(field) { 
    re = /(^\s*)([^\b]*\S)(\s*$)/; 
    if (re.test(document.getElementById(field).value)) { 
     document.getElementById(field).value = document.getElementById(field).value.replace (re, "$2"); 
     return document.getElementById(field).value; 
    } 
    else { 
     document.getElementById(field).value = ""; 
     return ""; 
    } 
} 

// The phone field shows how the user should input a phone number; 
// on the first focus, the value will be cleared so the user can 
// enter a phone number 
function clearField(field_id) { 
    if (first_time) { 
     document.getElementById(field_id).value = ""; 
     first_time = false; 
    } 
} 
+0

你到目前爲止有什麼? – Yacoby 2010-02-11 15:38:10

+0

任何代碼將不勝感激。 – Tim 2010-02-11 15:38:15

+0

請嘗試在「相關」下查看相關問題的右側。 – 2010-02-11 15:38:55

回答

2
  1. 你的AJAX會詢問一些GET或POST參數的PHP頁面(這些參數可以是這樣的搜索過濾器或任何其他人對你的項目非常感興趣)。你並不總是需要參數,但在大多數項目中通常需要一些參數。
  2. 此PHP頁面將根據這些參數查詢MySQL數據庫。
  3. 最後這個PHP頁面將在如XML,JSON可解析的格式輸出數據...或者你也可以輸出HTML direcly但它的移植性較差。
  4. 然後JavaScript解釋AJAX響應(PHP根據從MySQL接收到的數據輸出的數據)。

你可以在這裏看到Tizag或只是Google it教程...

+0

在我的答案中添加了一些教程。 – AlexV 2010-02-11 15:48:03

0

阿賈克斯僅僅意味着「使HTTP請求,而無需離開當前頁面」。它只關心網絡服務器和瀏覽器之間的通信。你仍然會得到一個正常的HTTP請求(在這種情況下,這可能導致PHP程序運行)。

連接到數據庫,並使用PHP從中獲取數據的問題是一個Ajax請求任何其他要求是相同的。

唯一的區別是,你可能會想比一個完整的HTML文檔等輸出的東西。

相關問題