這裏是一個工作代碼:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
function postRequest(server, dataToSend)
{
$.ajax({
url: server,
data: dataToSend,
type: "POST", //If you want to make a GET just change it here.
success: function(data){
//updatePage(data);
$("#result").append(data);
}
});
}
function comment(e)
{
var x=document.forms["myForm"]["name"].value;
var y=document.forms["myForm"]["email"].value;
var z=document.forms["myForm"]["message"].value;
postRequest("comment.php", {name : x, email : y, message:z }); //Send the url of the server, and the data
//Stop the default form submit
return false;
}
$(document).ready(function() {
$('#myForm').submit(comment);
});
</script>
</head>
<body>
<form id="myForm" name="myForm" method="GET" action=".">
<input type="text" name="name" /> <br />
<input type="text" name="email" /> <br />
<input type="text" name="message" /> <br />
<input type="submit" value="search"/>
</form>
<div id="result">
<h1>Data returned:</h1>
</div>
</body>
</html>
這看起來更像是你展示給我們什麼代碼,但我有一些建議,以改進的JavaScript代碼:
function postRequest(e)
{
$.ajax({
url: "comment.php",
data: $('form#myForm').serialize(),
type: "POST", //If you want to make a GET just change it here.
success: function(data){
//updatePage(data);
$("#result").append(data);
}
});
//Stop the default form submit
return false;
}
$(document).ready(function() {
$('#myForm').submit(postRequest);
});
如果你還想要了解更多關於Ajax和JavaScript與jQuery一般你可以看看一個這真的是一本好書:
http://jqfundamentals.com/book/index.html#chapter-7
是否AJAX請求根本不會發生,或者是你沒有看到'comment.php'正確的價值觀? –
如果所有信息都在URL中傳遞,那麼您可以使用HTTP GET。 – supertopi