您的 html 文件应该类似于:
<html>
<head>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript" >
$(document).ready(function(){
$("form#submit").submit(function() {
// we want to store the values from the form input box, then send via ajax below
var fname = $('#fname').attr('value');
var lname = $('#lname').attr('value');
$.ajax({
type: "POST",
url: "ajax.php",
data: "fname="+ fname +"& lname="+ lname,
success: function(){
$('form#submit').hide(function(){$('div.success').fadeIn();});
}
});
return false;
});
});
</script>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
<title>Insert title here</title>
</head>
<body>
<form id="submit" method="post">
<fieldset><legend>Enter Information</legend>
<label for="fname">Client First Name:</label>
<input id="fname" class="text" type="text" name="fname" size="20" />
<label for="lname">Client Last Name:</label>
<input id="lname" class="text" type="text" name="lname" size="20" />
<button class="button positive"> Add Client </button>
</fieldset>
</form>
<div class="success" style="display: none;">Client has been added.</div>
</body>
</html>
和 ajax.php
<?php
include ("../../inc/config.inc.php");
// CLIENT INFORMATION
$fname = htmlspecialchars(trim($_POST['fname']));
$lname = htmlspecialchars(trim($_POST['lname']));
$addClient = "INSERT INTO clients (fname,lname) VALUES ('$fname','$lname')";
mysql_query($addClient) or die(mysql_error());
?>
我从 [在此处输入链接描述][1] 复制此代码
[1]:http ://www.ryancoughlin.com/2008/11/04/use-jquery-to-submit-form/ 我测试过。