我有包含像下面的代碼元素的表形式:沒有選項,無法選擇。如何在JavaScript中調用數據庫中的數據?
<table border="1" bordercolor="#7695CC" id="table2">
<tr>
<td class="tablesubtitle" align="center">No</td>
<td class="tablesubtitle" align="center">Name Component</td>
<td class="tablesubtitle" align="center">Name child Component</td>
<td class="tablesubtitle" align="center"Point</td>
<td class="tablesubtitle" align="center">Action</td>
</tr>
<tr>
<td>1</td>
<td><select name="component" onChange="NameComponent(this);">
<option value="#"> </option>
<?php
opendb();
$query = "SELECT * FROM componentone";
$result = mysql_query($query);
while($data = mysql_fetch_array($result)){
$ID = $data['ID'];
$name = $data['Component'];
echo "<option value='$ID'>$name</option>";
}
?>
</select>
</td>
<td><div id='childcomponent'><select>
<option value="#"></option>
</select>
</div>
</td>
<td align="center"><a href="result_point.php?menu_id=<?=$menu_id?>">0</a></td>
<td align="center"><input type="button" value=" + " onClick="addRowToTable();"> | <input type="button" value=" - " onClick="removeRowFromTable();"></td>
</tr>
</table>
如果看到的onChange功能那裏,該函數是用於調用數據的結果從數據庫中顯示的結果(見div元素)<div id='childcomponent'>
選擇組件名稱<select name="component">
中的一個選項後。這兩個元素是連接的。
function NameComponent(combobox)
{
var code = combobox.value;
if (!code) return;
xmlhttp.open('get', '../template/get_component_name-opr.php?code='+code, true);
xmlhttp.onreadystatechange = function() {
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200))
{
document.getElementById("childcomponent").innerHTML = xmlhttp.responseText;
}
return false;
}
xmlhttp.send(null);
}
實施例:第1號,組件名我選擇是AAA,這AAA有三個childcomponent,是AAA1 AAA2 AAA3。在我選擇AAA後,三個子組件(AAA1,AAA2和AAA3)將自動顯示。 這是代碼,當我打電話,結果數據顯示給<div id='childcomponent'>
:
<?php
include ("config.php");
include ("dbfunctions.php");
echo "<script language=\"JavaScript\" src=\"js/form_validation.js\"></script>";
echo "<select name='child_component'>";
echo "<option value=\"\"> </option>";
opendb();
$query = "SELECT * FROM childcomponent WHERE ID='$_GET[code]' ";
$result = querydb($query);
while ($data = mysql_fetch_array($result))
{
echo "<option value='".$data['ID']."'>".$data['childcomponent_name']."</option>";
}
echo "</select>";
?>
在操作,我想添加行和刪除行。 這是通過使用Javascript功能來添加行的代碼:
<script language="Javascript">
function addRowToTable()
{
var tbl = document.getElementById('table2');
var lastRow = tbl.rows.length;
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
// left cell
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(iteration);
cellLeft.appendChild(textNode);
// right cell
var cellRightSel1 = row.insertCell(1);
var sel = document.createElement('select');
sel.name = 'component' + iteration;
cellRightSel1.appendChild(sel);
// select cell
var cellRightSel2 = row.insertCell(2);
var sel = document.createElement('select');
sel.id = 'childcomponent' + iteration;
sel.options[0] = new Option('','');
sel.options[1] = new Option('', '');
cellRightSel2.appendChild(sel);
var cellRight = row.insertCell(3);
var el = document.createElement('input');
cellRight.appendChild(el);
}
<script>
我在這裏的問題是,當我添加一行,兩數有根本沒有選擇,我可以在組件名稱選擇。 問題是,如何在JavaScript中調用數據庫中的數據? 從這裏調用數據庫,也許?
var cellRightSel2 = row.insertCell(2);
var sel = document.createElement('select');
sel.id = 'childcomponent' + iteration;
**sel.options[0] = new Option('','');
sel.options[1] = new Option('', '');**
cellRightSel2.appendChild(sel);
在此先感謝。
JS不會「調用數據庫」。 JS絕對沒有內置的數據庫功能。 JS **可以**通過AJAX在服務器上調用一個腳本,它將代表JS與數據庫進行通信。但這就像聲稱你製作了BigMac,因爲你在McD的櫃檯訂購了一臺。除此之外...漂亮的SQL注入漏洞。 – 2012-01-08 03:17:23
是否有什麼特別的原因,您不在這裏使用jQuery?如果你使用它,你的代碼可能會更簡單。 – bfavaretto 2012-01-08 03:18:44
當你做'addRow'時你想從數據庫獲取數據嗎?在這種情況下,您將不得不編寫另一個Ajax調用(類似於您爲'onChange'完成的調用),並使用響應中的數據構建錶行, – Nivas 2012-01-08 03:20:37