2013-02-06 101 views
0

我在我的項目中的某個時候感到困惑。 我想在這裏做的事情是如下:從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,所以我不能填充這個選擇框之前。

+0

使用AJAX來從數據庫獲取值並追加它。 –

+0

嗨Prasanth,我ve nevere之前使用阿賈克斯,所以你可以在這方面更具體 –

+0

你可以使用$ .get或$ .getJson裏面的JavaScript代碼來從MySQL獲取數據 – mfadel

回答

2

在這種情況下,您可以使用Ajax。

什麼是AJAX:

http://en.wikipedia.org/wiki/Ajax_(programming)

它提供了一個頁面,只是你以特定格式的信息。然後用你的javascript調用它,然後解析它到頁面中。

文檔如何在jQuery的做到這一點:

http://api.jquery.com/jQuery.ajax/


更新:

代碼示例:

$.get('ajax/test.html', function(data) { 

    // read in data = = [{'option':'option 1'},{'option':'option 2'}] 

     $.each(data, function(index, itemData) { 
     /// do stuff 
     $('<option>' + itemData.option + '</option>').appendTo('#myselectbox'); 
     }); 

}); 
+0

可以更具體的關於我的代碼,因爲我不熟悉AJAX那麼多 –

+0

好吧,讓我更新頁面:) –

+0

thanx很多,現在所有的作品,因爲我想。做得好 –

相關問題