我見過一些類似的問題,但我沒有看到具體說明這一點。我創建了一個非常簡單的示例,我覺得它應該可以工作,但事實並非如此。關鍵是要看到一些簡單的事情,以便其他類似的事情很清楚。如何讓我的jQuery和PHP與ajax一起工作?
我覺得這是非常「基本」的,很難簡單得多;因此,人們應該能夠得到它的後面,知道這是最終的noobie敲門磚:
的HTML和JS:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="javascript"></script>
<script type="text/javascript" src="/javascript/jquery-1.8.2.js">
$(document).ready(function(){
$("submit").click(function(){
var req = $.ajax({
type: 'POST',
url: 'form.php',
data: {
message: $('#message').val(),
author: $('#author').val()
},
timeout: 20000,
beforeSend: function(msg) {
$("#sent").html(msg);
}
});
req.fail(function(xhr, ajaxOptions, thrownError) {
alert("AJAX Failed");
});
req.done(function(res) {
$("#received").html(res);
});
});
});
</script>
</head>
<body>
<div id="sent"></div>
<div id="form">
<form>
Your message: <input type="text" name="message" value="Hi!" /><br />
Your name: <input type="text" name="author" value="Michael" /><br />
<input type="submit" name="submit" value="Submit me!" />
</form>
</div>
<div id="received"></div>
</body>
</html>
而且PHP:
<?php
echo "The file is located at ".$_POST["message"].".<br>";
echo "The file is named ".$_POST["author"].".";
什麼是或不是它在做什麼?從它的外觀來看,我懷疑它實際上是提交表單並重新加載頁面,而不是停止使用ajax請求。 –
@MichaelBerkowski我正在更新它的一些內容...截至目前,點擊提交不會做任何事情。我試圖看到發生的事情是點擊提交輸出數據到'#發送',然後到'#收到' –