我想要做什么
我想将用户名和分数发送到我的 php 页面,在那里它将数据插入数据库,然后吐出一个显示前十名分数的 html 字符串。
实际发生
了什么我输入用户名和分数,然后单击提交。数据被发送到php,然后将其存储在数据库中。然后,php 文件正确地构造了字符串,我可以通过直接访问 php 页面看到。但是,字符串不会返回到我的 JQuery。
$("#leaderboardForm > input[type=image]").click(function() {
var theName = $("input#leaderboardName").val();
var theScore = $("input#theScore").val();
$.ajax({
type: "POST",
url: "../admin/submitleaderboard.php",
data: { leaderboardName: theName, theScore: theScore },
dataType: "html",
success: function(leaderboard) {
$("div#deadStats").html(leaderboard);
},
error: function() {
alert("An error occured submitting your score.");
}
});
});
$name = $_POST['leaderboardName'];
$score = $_POST['theScore'];
$sql="INSERT INTO leaderboard (leaderboard_date,leaderboard_name,leaderboard_score) VALUES (CURDATE(),'$name','$score')";
$result = mysql_query($sql);
$sql="SELECT * FROM leaderboard";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
$leaderboard = $leaderboard . "<div style='width: 433px; padding: 8px; overflow: hidden; border: 1px solid #300; margin-bottom: 5px;'>";
$leaderboard = $leaderboard . "<div style='width: 200px; height: 30px; float: left;'>";
$leaderboard = $leaderboard . "<span style='color: #EFEFEF; font-weight: bold; font-size: 20px;'>" . $row['leaderboard_name'] . "</span></div>";
$leaderboard = $leaderboard . "<div style='width: 200px; height: 30px; float: left;'>";
$leaderboard = $leaderboard . "<span style='color: #A00; font-weight: bold; font-size: 20px;'>" . $row['leaderboard_score'] . "</span></div>";
$leaderboard = $leaderboard . "</div>";
}
echo $leaderboard;
mysql_close();