我想要一个表单,可以让我添加名称到我的数据库,而无需刷新窗体。我一直在努力,但似乎并不奏效。我很新,所以任何帮助表示赞赏。将表单数据添加到mysql数据库PHP和AJAX
对于我的index.html我有:
$(function() {
$(".submit").click(function() {
var name = $("#firstname").val();
var username = $("#lastname").val();
var dataString = 'firstname='+ firstname + 'lastname=' + lastname
if(firstname=='' || lastname=='')
{
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
}
else
{
$.ajax({
type: "POST",
url: "join.php",
data: dataString,
success: function(){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
Please Enter Valid Data
Registration Successfully
对于join.php:
include("db.php");
if($_POST)
{
$firstname=$_POST['firstname'];
$lastname=$_POST['username'];
mysql_query("INSERT INTO persons (firstname,lastname) VALUES('$firstname','$lastname')");
}
?>
和db.php中:
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "test";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
?>
2013-06-05
hmmm
+0
你得到按钮的点击什么反应 –