我做了一個搜索,並檢查了這個網站在寫沒有成功的時候給我的一些建議。這裏是我的問題: 在我的頁面上,我通過AJAX加載所有鏈接,其中一個頁面用於從mysql加載數據的配置文件。在個人資料頁面的底部,您可以輸入代表時間的數字。當我這樣做時,我希望頁面用輸入的數字更新數據庫並更新當前頁面。由於我使用AJAX,因此我無法添加一個我希望它進入或重新加載整個頁面的url,因爲這樣我就會在開始頁面結束。通過html格式調用php函數
我已經做了一個名爲「add_time()」的函數,我想調用它來計算和更新數據庫,我無法通過我的表單加載數據庫,沒有我不幸意識到的一些技巧。
我也想知道如何將它與我的AJAX load_page(url)-script結合起來,因此它使得這兩個動作同時發生。該div沒有設置,但我已經在交換機所在的配置文件頁面中標記。
<?php
$user = $_COOKIE["user"];
$host = "//";
$username = "//";
$password = "//";
$db_name = "//";
$tbl_name = "user_stats";
mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name") or die("cannot select DB");
$sql = "SELECT * FROM $tbl_name WHERE username='$user'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if ($count == 1) {
while ($row = mysql_fetch_array($result)) {
$user = $row[username];
$level = $row[level];
$hours = $row[hours];
}
}
echo "<h1>$user</h1>";
?>
<table id="profile_form">
<tr>
<td id="profile_username">Användare: <?php echo $user; ?></td><td id="profile_level"><?php echo "Level: " . $level . "<br />Hours: " . $hours; ?></td>
</tr>
<tr>
<td id="profile_picture">
Profil picture.
</td>
</tr>
<tr>
<td id="profile_title">
Title:
</td>
</tr>
</table>
<!-- Div-break. Guess we just call the upper part "profile" and the lower part "add_time" for now.-->
<form action="" method="post">
Lägg till tid: <input type="text" name="new_time" />
<input type="hidden" name="prev_time" value="<?php echo $time; ?>" />
<input type="submit" value="Lägg till tid" />
</form>
從外部文件調用的函數:
的個人資料頁
function add_time() {
$new_time = $_POST["new_time"] + $_POST["prev_time"];
$user = $_COOKIE["user"];
$host = "//";
$username = "//";
$password = "//";
$db_name = "//";
$tbl_name = "user_stats";
$con = mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name") or die("cannot select DB");
$sql = mysql_query("UPDATE user_stats SET hours = '$new_time'
WHERE username = '$user'");
mysql_close($con);
echo $new_time . "<br />";
}
我希望它能幫助我發送驗證碼。現在很亂,我很抱歉。我想我已經挖了這麼多,我可能會讓自己感到困惑,並且在圈子裏跑來跑去,所以我可以再次深入這個兔子洞。
我有用於加載我的頁面的ajax代碼。所以這裏是列表: 1.從窗體調用一個php函數,或者以相同的方式工作。 2.更新頁面而不重新開始。 如果我可以在不改變網址的情況下做到這一點,我將非常高興。我正在做一個小實驗,我可以做多少事情而不必這樣做。 – 2011-05-09 21:12:09
返回false;這裏是關鍵球員。 $('a')。click(function(){$(「#PLACEHOLDER_DIV」)。load('ajax.php?function = addtime'); return false;}); – Zalaboza 2011-05-13 11:34:44