我想從我的數據庫中檢索多行數據。將返回的行數是未知的。它適用於返回的一行。但我也有多行返回到HTML頁面的場景,其中ajax函數將相應的值添加到窗體。Ajax PHP HTML表格
處理html表單中未知行數的最佳方法是什麼?這個形式也是一個好主意?
我試圖找出一種方法讓PHP scipt生成html代碼並使用ajax將其傳回。但一直無法在網上找到任何例子。
在該函數中,您可以看到我獲取數據並將其等同於表單。該表格預定義了一個條目。但我需要知道如何修改表單以知道將返回多少條記錄。我希望這是有道理的。先進的謝謝你。
CODE:
function getFunction(){
//browser support code
var ajaxRequest; // The variable to create the ajax request
try {
// Opera 8.0+, Firefox, Safari Support
ajaxRequest = new XMLHttpRequest();
} catch (e){
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong with the browser support
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
// document.write(ajaxRequest.responseText);
var response = JSON.parse(ajaxRequest.responseText);
var pfullname=response.pfirstname + " ";
var dfullname=response.dfirstname + " ";
if (response.pmiddlename!=null) {pfullname = pfullname + response.pmiddlename + " ";}
pfullname = pfullname + " " + response.plastname;
document.info.name.innerHTML = pfullname;
//document.info.address.value = ajaxRequest.responseText;
document.info.dob.innerHTML = response.dob;
document.info.address.innerHTML = response.paddress;
document.info.phonenumber.innerHTML = response.phonenumber;
document.info.sex.innerHTML = response.sex;
document.info.occupation.innerHTML = response.occupation;
if (response.dmiddlename!=null) {dfullname = dfullname + response.dmiddlename + " ";}
dfullname = dfullname + response.dlastname;
document.info.doctorp.innerHTML = dfullname;
document.appointment.locationa.innerHTML = response.alocation;
document.appointment.datea.innerHTML = response.adate;
document.appointment.doctora.innerHTML = dfullname;
}
}
//var testname = document.getElementById('testname').value;
var healthid = document.getElementById('healthid').value;
//var queryString = "?testname=" + testname + "&testpassword=" + testpassword;
var queryString = "healthid=" + healthid;
//document.write(queryString);
ajaxRequest.open("POST", "getnum.php", true);
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.send(queryString);
}
PHP CODE:
查詢(SQL $)){ 而($行= $ result-> fetch_object()){ 回波json_encode($行); } } else { //發生錯誤 echo'error:'。$ con> error; } mysqli_close($ con); ? >HTML CODE:
<h2>
<span style="font-weight:bold;">Patient Information:</span>
<form name='info'>
Name: <output type='text' name="name" id="name"> </output><br>
DOB: <output value='' type='text' name="dob" id="dob"> </output><br>
Address: <output type='text' name="address" id="address"> </output><br>
Phone Number: <output type='text' name="phonenumber" id="phonenumber"> </output><br>
Sex: <output type='text' name="sex" id="sex"> </output><br>
Occupation: <output type='text' name="occupation" id="occupation"> </output><br>
Doctor: <output type='text' name="doctorp" id="doctorp"> </output><br>
Doctor Address: <output type='text' name="doctoradd" id="doctoradd"> </ouput><br>
</form>
</h2>
哪裏是你的PHP代碼? –
您可以在responseText中以json格式發送多個記錄並遍歷它們。你可以發佈你的表單HTML也 –
我已經添加了PHP和HTML的代碼。問題是我的表單是靜態的,但我需要它是動態的,所以如果有多個病人,那麼我可以用多行返回的PHP代碼列出兩組這些表單。 – user3126529