[英]Populate select2 dropdown with selected value
I am using a PHP script to edit data that is stored in my database. 我正在使用PHP脚本来编辑存储在数据库中的数据。 The data is stored with dynamic rows.
数据与动态行一起存储。
In my script I am also using the Select2 script that makes it possible to search in my dropdown. 在我的脚本中,我还使用了Select2脚本,该脚本可以在下拉菜单中进行搜索。
The Ajax script I am using to populate is the following: 我正在使用的Ajax脚本如下:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function getName<?php echo $m; ?>() {
$.ajax
({
type: 'POST',
url: './get/name/get1.php',
dataType: 'text',
cache: false,
success: function(data){
$('#name<?php echo $m; ?>').html(data);
},
complete: function(){}
});
}
$(document).ready(function(){
getName<?php echo $m; ?>();
});
</script>
In get1.php
I an using the following statement to select the data: 在
get1.php
我使用以下语句选择数据:
SELECT internal_id, name FROM scu_stock
The scu_stock table has the following data: scu_stock表具有以下数据:
-------------------------
| internal_id | name |
| 1 | One |
| 2 | Two |
| 3 | Three |
| 4 | Four |
-------------------------
The value that I want to represent as the selected value
is stored in another table. 我要表示为
selected value
存储在另一个表中。 scu_address scu_address
---------------------------------------
| id | city | country | internal_id |
| 6 | aa | ww | 1 |
| 7 | bb | xx | 2 |
| 8 | cc | yy | 3 |
| 9 | dd | zz | 4 |
---------------------------------------
When I open index.php?id=8
I want to see the the data in table scu_stock with the selected value that is called in the scu_address table. 当我打开
index.php?id=8
我想查看表scu_stock中的数据以及在scu_address表中调用的选定值。
Does someone know how I can realize this? 有人知道我该怎么实现吗?
Here is my full code: 这是我的完整代码:
<?php
$statement = $connect->prepare("SELECT * FROM scu_address WHERE id = :id");
$statement->execute(
array(
':id' => $_GET["id"]
)
);
$item_result = $statement->fetchAll();
$m = 0;
foreach($item_result as $sub_row)
{
$m = $m + 1;
?>
<tr id="row_id_<?php echo $m; ?>">
<td><label for="name">Name</label></td>
<td><select style="width:100%" id="name<?php echo $m; ?>" name="name[]" data-srno="<?php echo $m; ?>"></select></td>
<script type="text/javascript" src="../../../../vendors/select2/dist/js/select2.min.js"></script>
<script type="text/javascript">
$.fn.select2.defaults = $.extend($.fn.select2.defaults, {
allowClear: true,
closeOnSelect: true,
value: 'Name',
minimumResultsForSearch: 15
});
$(document).ready(
function () {
var configParamsObj = {
placeholder: 'Name',
minimumResultsForSearch: 3
};
$("#name<?php echo $m; ?>").select2(configParamsObj);
});
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function getName<?php echo $m; ?>() {
$.ajax
({
type: 'POST',
url: './get/name/get1.php',
dataType: 'text',
cache: false,
success: function(data){
$('#name<?php echo $m; ?>').html(data);
},
complete: function(){}
});
}
$(document).ready(function(){
getName<?php echo $m; ?>();
});
</script>
</tr>
<?php
}
?>
To fill the select2 textbox with selected value fetched from the database: 用从数据库中获取的选定值填充select2文本框:
$selecteddata = " ";
$selecteddata = " ";
then fill it to obtain the following format $selecteddata .="'1','2','3','4'";
$selecteddata .="'1','2','3','4'";
After the select2 code add the following script: 在select2代码之后,添加以下脚本:
<script>
$('#select2ID').val([<?php echo $selecteddata ;?>]);
$('#select2ID').trigger('change');
</script>
By this you will select only the data you fetched so you can edit. 这样,您将仅选择获取的数据,以便进行编辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.