【发布时间】:2010-10-07 17:18:47
【问题描述】:
我正在尝试使用 php 和 jQuery 制作一个简单的 MySQL Admin。我从未使用过 jQuery,所以我的代码可能非常值得大笑。我遇到的代码问题是,当我单击按钮时,什么也没有发生。我知道偶数会触发,因为如果我在 firefox 中打开 html 文件(不转到 url,使用 file:/// 东西)并单击它,它会在我希望返回的内容进入的框中显示我的 php 代码.我要做的第一件事是连接到指定的数据库并返回表列表。这是我的代码
index.html
<html>
<head>
<script type='text/javascript' src='jquery.js'></script>
<script type='text/javascript'>
var Server = 'None';
var Username = 'None';
var Password = 'None';
var Database = 'None';
$("#connect").click(function() {
Server = $('#server').val();
Username = $('#username').val();
Password = $('#password').val();
Database = $('#database').val();
loadTables();
});
function loadTables() {
$.get("display.php", { server: Server, username: Username, password: Password, database: Database, content: "tables" },
function(data){
html = "<ul>";
$(data).find("table").each(function() {
html = html + "<li>" + $(this).text() + "</li>";
});
html = html + "</ul>";
$('#content').html(html);
}
);
}
</script>
</head>
<body>
<center>
<div class='connection'>
<form name='connectForm'>
Server: <input type='text' size='30' id='server' />
Username: <input type='text' id='username' />
Password: <input type='password' id='password' />
Database: <input type='text' id='database' />
<input type='button' id='connect' value='Connect' />
</form>
</div>
<div id='content'>
</div>
</center>
</body>
</html>
display.php
<?
mysql_connect($_GET['server'], $_GET['username'], $_GET['password'])
or die("Error: Could not connect to database!<br />" . mysql_error());
mysql_select_db($_GET['database']);
$content = $_GET['content'];
if ($content == "tables") {
$result = mysql_query("show tables");
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$xml .= "<tables>";
while ($row = mysql_fetch_assoc($result)) {
$xml .= "<table>" . $row['Tables_in_blog'] . "</table>";
}
$xml .= "</tables>";
header('Content-type: text/xml');
echo $xml;
}
?>
编辑: 我已经根据几个答案的组合更新了代码,但我仍然遇到同样的问题。
【问题讨论】:
标签: php javascript jquery mysql