我从 YouTube 教程中获取了以下代码(来源:pbj746)。除了提交的数据没有出现在数据库中之外,一切似乎都在工作!我检查了 $dbhost、$db、$dbuser 和 $dbpassword,它们都匹配,但数据没有被拉入。
如果有人可以提供帮助,那就太好了!
<?php
$dbServer = "localhost";
$db = "quizsoft_newsletter";
$dbUser = "admin_newsletter";
$dbPassword = "password";
$conn = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);
$email = $_POST['email'];
mysql_query("INSERT INTO emails ('ID', 'Email')
VALUES(NULL, '$email')") or die(mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Form</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(e) {
$("div#msgbox").hide();
$("#addEmail").click(function() {
$.post('process.php', {
email:$("#email").val()
},
function(response) {
$("div#msgbox #msg").html("Email Submitted Successfully!")
$("div#msgbox").slideDown("normal");
}
);
return false;
});
$("div#msgbox").click(function() {
$(this).slideUp("normal");
})
});
</script>
<style>
body { font:Verdana, Geneva, sans-serif; font-size:13px; }
div#msgbox { background:#999; border-bottom:1px solid #CCC; color:#FFF; width:100%; height:30px; top:0; left:0; cursor:pointer; }
div#msgbix #msg { line-height:30px; padding-left:10px; }
form#newsletter { margin-top:35px; }
</style>
</head>
<body>
<div id="msgbox"><span id="msg"> </span></div>
<form id="newsletter">
Email:<input type="text" id="email" />
<input type="submit" id="addEmail" value="Submit" />
</form>
</body>
</html>
感谢:D!
编辑:此外,我运行了一个简单的 PHP 脚本来确认数据库连接:http: //quizsoft.co.uk/ajaxform/test.php