我正在尝试用 php 和 jQuery 做一个简单的 MySQL 管理。我从未使用过 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>
显示.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;
}
?>
编辑: 我已经根据几个答案的组合更新了代码,但我仍然遇到同样的问题。
最佳答案
好的,首先不要那样做,我所说的“那样”是指:
- 不要在 Javascript 中放入数据库连接细节;和
- 不要使用未经过滤的用户输入。你只是要求被黑客攻击。
也就是说,您的主要问题似乎是 $(#content)
应该是 $("#content")
。另外,在按钮上放置一个 onclick 并不是真正的 jQuery 方式。尝试:
<body>
<div class='connection'>
<form name='connectForm'>
Server: <input type='text' id="server" size='30' name='server' />
Username: <input type='text' id="username" name='username' />
Password: <input type='password' id="password" name='password' />
Database: <input type='text' id="database" name='database' />
<input type='button' id="connect" value='Connect' />
</form>
</div>
<div id='content'></div>
<script type="text/javascript" src="/jquery-1.3.1.js"></script>
<script type="text/javascript">
$(function() {
$("#connect").click(function() {
$.get(
"display.php",
{
server: $("#server").val(),
username: $("#username").val(),
password: $("#password").val(),
database: $("#database").val(),
content: "tables"
},
function(data) {
$("#content").html("<ul></ul>");
$(data).find("table").each(function() {
$("#content ul").append("<li>" + $(this).text() + "</li>");
});
}
);
});
});
</script>
</body>
编辑: 对上述内容进行小的更正并使用此脚本进行测试:
<?
header('Content-Type: text/xml');
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<root>
<?
$tables = array('one', 'two', 'three', 'four');
foreach ($tables as $table) {
echo " <table>$table</table>\n";
}
?>
</root>
关于php - 开始 jQuery 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/550494/