0
我試圖在使用PHP腳本的wordpress網站上設置自動完成。但是目前我的代碼中沒有顯示任何內容。我明白,一般的想法是有一個jQuery函數,它會使用PHP腳本來提取MySQL數據(suggest.php)。另外如果我要把WordPress中的jQuery自動完成
<script>
$(function() {
$("#tags").autocomplete({
source: 'suggest.php',
minLength:1
});
});
</script>
在myScript.js下的js文件夾下,我將如何訪問它?我的完整代碼如下...
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<script>
$(function() {
$("#tags").autocomplete({
source: 'suggest.php',
minLength:1
});
});
</script>
<form action="" method="post">
Name: <input type="text" name="tags" id="tags" value="<?php echo isset($_POST['tags']) ? $_POST['tags'] : '' ?>"/>
</form>
因此,根據這個解決方案,我們將設置函數,它將利用MySQL查詢文件中的results_array作爲functions.php中的suggest.php並設置custom.js? 我怎麼enque custom.js在PHP頁面? 這個參數對於 的參數「wp_enqueue_script案件? – findataguy
我不確定你的'suggest.php'文件的內容是什麼,但'$ results_array'需要是一個數組。無論您是手動編寫該數組,還是創建將以數組形式返回結果的查詢都不重要。讓數組傳遞給JavaScript文件非常重要。 enqueue部分在我的代碼中給出。將它粘貼到你的'functions.php'中,你將有'custom.js'入隊(確保它位於指定位置)。我忘了用'get_template_directory_uri()'函數替換'TEMPPATH'常量。我現在要修復它 –
所以基本上我覺得有了基本的想法並設置了自動完成。但我有兩個問題。其中一個我無法從'suggest.php'獲得源代碼,其中'json-encoded'列表將被'echo'。如果我要手動輸入數組,自動完成列表不會在第一次輸入時顯示,但如果我點擊提交按鈕,它就會顯示出來。基本上我的custom.js文件看起來像這樣 – findataguy