如果我理解正確的話,你想有一個網頁,當用戶點擊一個鏈接就可以從數據庫中獲取一些數據,並使用AJAX/jQuery的
添加一個PHP文件在您根警報顯示它與
<?php
//edit for your server/database
$username = "username";
$password = "password";
$hostname = "hostname";
$dbname = "database-name";
//to connect to the database
$db = new PDO('mysql:host='.$hostname.';dbname='.$dbname.';charset=utf8', $username, $password);
?>
目錄這是很簡單的說真的,如果你需要數據很多次,那麼你很可能做錯事排序或你可以用JOIN子句嘗試。
假設你要顯示的用戶點擊該鏈接(並從數據庫讀取的鏈接),你不妨在另一個PHP文件中添加php_file_tree功能這樣
<?php
if(isset($_GET['Phrase'])){
$needle = $_GET['Phrase'];
}else{$needle = null;}
function php_file_tree($directory, $javascript){
//include previous file with connection details
global $needle, $db;
//I'm not sure what you are looking for in the database
$value = "value"
//could be ASC or DESC $needle or $value
$query = "SELECT table.Phrases FROM table WHERE Phrases = '{$needle}' ORDER BY A DESC;";
$sth = $db->prepare($query);
$sth->execute();
$data = $sth->fetchAll();
return $javascript = preg_replace('/\[\[.*?\]\]/', 'link', $javascript);
}
//should give you the alert
echo php_file_tree($_SERVER['DOCUMENT_ROOT'], "javascript:alert('You clicked on [link]');");
?>
也添加到您的HTML頁面
<script src="jquery.js" type="text/javascript"></script>
<script src="php_file_tree_jquery.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
var fetchUrl = 'path-to-the-php-file-with-the-function'
var fetchNeedle = $("#link").html();
$.get(fetchUrl.concat(fetchNeedle)).done(function(data) { Alert(data); });
});
</script>
參考:http://deocasion.org/blog/ajax-dropdown/
此外,該教程是爲C#和你正在做它在PHP – octohedron 2015-02-09 11:23:04