我在我的項目中的某個時候感到困惑。 我想在這裏做的事情是如下:從MySQL數據庫中填充動態添加的選擇框
從數據庫中獲取 數據與按鈕創建一個新的選擇框 - 從數據庫中創建 填充值這個選擇框 選項刪除該選擇框通過按鈕 - 刪除
我到目前爲止PHP/HTML代碼:
<button type="button" name="add" onclick="return false;" id="addField" class="nice_button">Добавить(ADD)</button>
<button type="button" name="remove" onclick="return false;" id="remove" class="nice_button">Удалить(DELETE)</button>
<tbody id="documentFields">
<tr>
<td>
<select size="1" name="hardware[]" style='width:400px;'>
<option value=""></option>
<?php while($hardware = $get_hardware->fetch_array()){
echo "<option value='".$hardware['st_id']."'>".$hardware['st_name']." ".$hardware['st_producer']." ".$hardware['st_model']."</option>";
}
?>
</select>
</td>
</tr>
</tbody>
JS腳本到目前爲止我看起來像這樣:
var g = 1;
var id = [ <? php echo $js_id; ?> ];
$(document).ready(function Product_add() {
$("#addField").click(function() {
$("#documentFields").append('<tr id="fieldset_p' + g + '"><td><select size="1" name="hardware[]" style="width:400px;"><option value=""></option></select></td></tr>');
g++;
});
});
$(document).ready(function Product_add() {
$('#remove').click(function() { // similar to the previous, when you click remove link
if (g > 1) { // if you have at least 1 input on the form
g--; //deduct 1 from i so if i = 3, after i--, i will be i = 2
$('#fieldset_p' + g + '').remove(); //remove the last fieldset
}
});
});
事情是,我不能附加PHP函數:while,所以我不能填充這個選擇框之前。
使用AJAX來從數據庫獲取值並追加它。 –
嗨Prasanth,我ve nevere之前使用阿賈克斯,所以你可以在這方面更具體 –
你可以使用$ .get或$ .getJson裏面的JavaScript代碼來從MySQL獲取數據 – mfadel