我正在努力让它发挥作用,但我遇到了问题。我在这里缺少什么?
我正在尝试通过 jQuery 将一些数据插入到本地 MySQL 表中。如果我自己运行 save.php,它会在数据库中插入一个空白行,这样就可以了。有什么想法吗?
**index.php**
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('form#submit').submit(function () {
var nume = $('#nume').val();
$.ajax({
type: "POST",
url: "save.php",
data: "nume="+ nume,
success: function() {
$('#nume').val('');
}
});
return false;
});
});
</script>
</head>
<body>
<form id="submit" method="post">
<p>Nume: <input id="nume" name="nume" type="text"></p>
<p><input id="submitButton" type="button" value="Submit"></p>
</form>
</body>
</html>
**save.php**
<?php
mysql_connect('localhost', 'root', 'root') or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$nume = htmlspecialchars(trim($_POST['nume']));
$add = "INSERT INTO ajax_test (nume) VALUES ('$nume')";
mysql_query($add) or die(mysql_error());
?>
最佳答案
您确定表单正在提交吗?您的按钮不是 input type="submit"- 它只是一个按钮。它不会自行提交表单。
关于php - 使用 jQuery 向 MySQL 表中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1759577/