2017-07-08 32 views
0

我在使用PHP的項目中創建了這個引導程序輸入標記。如何使用自動完成功能將我的MySQL數據編碼到Bootstrap輸入標籤中?

現在,我如何使用MySQL中的數據填充restrictTosuggestions

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>Send Memorandum</title> 
    <!-- Bootstrap --> 
    <link href="css/bootstrap.min.css" rel="stylesheet"> 
    <link href="css/memo.css" rel="stylesheet"> 
    <link href="css/bootstrap-tags.css" rel="stylesheet"> 
    <script src="js/jquery.min.js"></script> 
</head> 

<body> 
<form enctype="multipart/form-data" action="send.php" method="post" class="memo" style="padding-top:10px"> 
    <div class="form-group row"> 
     <div class="col-xs-8"> 
      <label for="To" style="padding-top: 10px;" >To: </label><br> 
      <p>suggestOnClick</p> 
      <div id="suggestOnClick"></div> 
     </div> 
    </div> 
</form> 

<script src="js/bootstrap.min.js"></script> 
<script src="js/bootstrap-tags.min.js"></script> 

在本節我想使的建議默認值:被移除,然後用我的MySQL數據值,如「選擇account_username FROM tbaccounts」入建議更換:和restrictTo:這是可能的?

所以這是我的腳本

<script> 
$(function(){ 
    $("#suggestOnClick").tags({ 
     restrictTo: ["alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel", "india"], 
     suggestions: ["alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel", "india"], 
     promptText: "Click here to add new tags", 
     suggestOnClick: true 
    }); 
}); 
</script> 

</body> 
</html> 

這是我的PHP

<?php 
//fetch.php 
$connect = mysqli_connect("localhost", "root", "", "fullcalendar"); 
$query = " 
SELECT * FROM tbaccounts ORDER BY account_username 
"; 

$result = mysqli_query($connect, $query); 

$data = array(); 

if(mysqli_num_rows($result) > 0) 
{ 
while($row = mysqli_fetch_assoc($result)) 
{ 
    $data[] = $row["account_username"]; 
} 
echo json_encode($data); 
} 

?> 

現在,我怎樣才能把這個json_enccode($的數據);限制爲:和Suggstions:在SuggestOnClick函數中?

+0

只是做一個查詢來獲取標籤和打印出來作爲使用'json_encode JSON陣列()'。你的PHP在哪裏?你做了DB查詢嗎? –

+0

對不起@MagnusEriksson,但我不現在如何使用json_encode(),但你可以顯示或幫助我如何做到這一點? 我有我的數據庫名爲fullcalendar,然後我想取我的表名爲'tbaccouts'的數據tbaccounts有account_id,andaccount_username的數據。 account_username是我想要放在restrictTo和Suggestion腳本中的數據。 你能幫我解答嗎? –

+1

添加_a​​ll相關code_到您的問題。不要發表評論。這是不可讀的。關於PHP函數的使用,首先檢查[PHP手冊](http://php.net/) –

回答

0

您可以隨時使用jquery ajax。

JS文件

var app={ 
    init:function(){ 
     $(document).ready(this.initialize); 
    }, 
    initialize:function(){ 
     $.ajax({ 
      url:'php/fetch.php', 
      METHOD:'GET', 
      success:function(data){ 
       data=JSON.parse(data); 
       $("#suggestOnClick").tags({ 
        restrictTo: data, 
        suggestions: data, 
        promptText: "Click here to add new tags", 
        suggestOnClick: true 
       }); 
      } 
     }); 

    } 
}; 
app.init(); 

PHP/fetch.php

echo json_encode(array("alpha","bravo","charlie"));

+0

非常感謝你的工作! –

相關問題