我使用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;
}
}
你到目前爲止有什麼? – Yacoby 2010-02-11 15:38:10
任何代碼將不勝感激。 – Tim 2010-02-11 15:38:15
請嘗試在「相關」下查看相關問題的右側。 – 2010-02-11 15:38:55