我正在使用PHP從MySQL數據庫中檢索有關「資源」的信息。關於每個資源的信息是作爲HTML輸出像下面這樣的格式:在同一頁面上的多個相似的窗體
Title
Description
Etc.
然後每個資源都有一個「編輯」鏈接,運行的JavaScript的onclick。該JS插入一個包含所選資源信息的表單,以便在數據庫中進行編輯和更新。如果JavaScript被禁用,用戶將被帶到一個單獨的頁面,該頁面只有編輯該特定資源的表單。所以,我的問題是:如何在同一頁面上管理任意數量的表單,這些表單都具有相同的功能,僅適用於MySQL數據庫的不同行(不同的「資源」)?特別是,我將如何確定要在JavaScript插入的表單中編輯的特定資源?
現在我正在使用一個隱藏的輸入來攜帶資源ID(erid),但PHP腳本看起來像什麼實際上處理表單動作?出現這個問題的原因是因爲輸入類型的每個名稱=「提交」當前都攜帶了erid,例如, NAME = 「UPDATE1」,名字= 「UPDATE2」 等
從數據庫中獲取資源:
require('inc/db_connect_info.config');
$mysqli = new mysqli($host, $mysqlilogin, $mysqlipassword, $databaseName);
if (mysqli_connect_error()) {
die("Can't connect to database: " . mysqli_connect_error());
}
else{
$testQuery = "SELECT * FROM eresources NATURAL JOIN subjects";
$testResult = $mysqli->query($testQuery);
if($testResult && $testResult->num_rows >= 1) {
while($testArray = $testResult->fetch_assoc()) {
$testArray['stitle'] = htmlentities($testArray['stitle']);
// START er div
print("<div id=\"er".$testArray['erid']."\" class=\"grey\">"); print("\n\n\t");
// erid, link, title, descr
print("<p>".$testArray['erid'].": <a id=\"link".$testArray['erid']."\" href=\"".$testArray['link']."\">".$testArray['ertitle']."</a> - <span id=\"descr".$testArray['erid']."\">".$testArray['descr']."</span></p>");
print("\n\t");
// edit "link"
print("<a href=\"edit.php?erid=".$testArray['erid']."\" onclick=\"return showEdit(".$testArray['erid'].")\">Edit</a>");
print(" ");
// END er div
print("</div>");
print("\n\n");
}
}
// If there was a problem retrieving resources, print an error message //
else{
print("<p>No e-resources found!</p>");
}
$mysqli->close();
}
JavaScript函數網頁上插入更新的形式在那裏,如果 「編輯」 鏈接被點擊 (假設啓用JavaScript):
function showEdit(erid) {
//get page url for form submission
var url = document.location.href;
//current title
var erTitle = document.getElementById("link" + erid).innerHTML;
//current link
var erLink = document.getElementById("link" + erid).getAttribute("href");
//current description
var erDescr = document.getElementById("descr" + erid).innerHTML;
//current subject under which electronic resource is filed
var erSubject = document.getElementById("subject" + erid).innerHTML;
//clear the er div
document.getElementById("er" + erid).innerHTML = "";
//then fill it with a form for new values, to be submitted to the same page
//the form is prepopulated with the current values from the database
document.getElementById("er" + erid).innerHTML =
"<form action=\"" + url + "\" method=\"post\">" +
"Title<br /><input type=\"text\" name=\"updated_er_title"+erid+"\" value=\"" + erTitle + "\" size=\"100\" /><br /><br />" +
"Link<br /><input type=\"text\" name=\"updated_er_link"+erid+"\" value=\"" + erLink + "\" size=\"100\" /><br /><br />" +
"Description<br /><textarea name=\"updated_er_descr"+erid+"\" cols=\"75\" rows=\"10\">" + erDescr + "</textarea><br /><br />" +
"Free Status<br />" + erFree + "<br /><br />" +
"Subject<br /><input type=\"text\" name=\"updated_er_subject"+erid+"\" value=\"" + erSubject + "\" size=\"100\" /><br /><br />" +
"<input type=\"hidden\" name=\"erid\" value=\""+erid+"\" />" +
"<input type=\"submit\" name=\"update" + erid + "\" value=\"Update\" />" +
"</form>";
// return false so the link is not followed if javascript is enabled
// if javascript is disabled, link provides same form on separate page
return false;
}