HTML提交表单无刷新页面并显示提示框
我想提交表单到php,然后数据将更新到数据库,并显示提示框。 现在,数据已更新数据库,但警报框未显示。 我的代码有什么问题? 谢谢。HTML提交表单无刷新页面并显示提示框
的test.html
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function SubmitForm() {
var category_id = $("#category_id").val();
var category_name = $("#category_name").val();
$.post("test.php", { category_id: category_id, category_name: category_name },
function(data) {
alert("Finish!");
});
}
</script>
</head>
<body>
<form action="test.php" method="post">
category_id: <input type="text" name="category_id" id="category_id"/>
category_name: <input type="text" name="category_name" id="category_name"/>
<input type="button" id="searchForm" onclick="SubmitForm();" value="Send" />
</form>
</body>
</html>
test.php的
<?php
$a = $_POST['category_id'];
$b = $_POST['category_name'];
$link = mysql_connect('localhost','root','');
mysql_select_db("fyp", $link);
$sql = "INSERT INTO category (CID, Category) VALUES (".
PrepSQL($a) . ", " .
PrepSQL($b) . ")";
mysql_query($sql);
function PrepSQL($value)
{
// Stripslashes
if(get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
// Quote
$value = "'" . mysql_real_escape_string($value) . "'";
return($value);
}
?>
试试这个
function SubmitForm() {
var category_id = $("#category_id").val();
var category_name = $("#category_name").val();
$.post("test.php", { category_id: category_id, category_name: category_name })
.done(function(data) {
alert("Finish!");
});
}
1)他使用的是按钮,而不是提交。 2)他的问题甚至不抱怨重定向。 – Prisoner 2013-03-19 14:21:44
试试这个:
$.post("test.php", { category_id: category_id, category_name: category_name })
.done(
function(data) {
alert("Finish!");
}
);
谢谢。但警告框仍然没有弹出... – user2108245 2013-03-19 14:35:04
尝试链接'.fail(function(){alert(“error”);})'结束查看表单是否有错误 – Pete 2013-03-19 14:35:44
弹出错误这一次 – user2108245 2013-03-19 14:43:33
你把三个参数放在那里,它应该是两个。你可以试试这个:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function SubmitForm() {
var category_id = $("#category_id").val();
var category_name = $("#category_name").val();
$.post("test.php", { category_id: category_id, category_name: category_name })
.done(function() {
alert("Finish!");
});
}
</script>
祝你好运!
谢谢。但警报框仍然没有弹出... – user2108245 2013-03-19 14:33:56
尝试这种解决方案
function SubmitForm() {
var category_id = $("#category_id").val();
var category_name = $("#category_name").val();
$.ajax({
type: "POST",
url: "test.php",
data: { category_id: category_id, category_name: category_name }
}).done (function() {
alert("Finish");
}).fail (function(status) {
console.log(status);
});
现在检查数据,看看什么错误。
谢谢。但仍没有弹出。 我试图添加一个.fail()和显示的失败弹出窗口。 – user2108245 2013-03-19 14:58:02
是的,那么问题是响应对象没有返回成功。尝试console.log(数据)在失败。我正在编辑我的答案来做到这一点。 – theshadowmonkey 2013-03-19 18:35:25
使用.done()获取成功或.fail()获取失败的响应 – theshadowmonkey 2013-03-19 14:22:04
为什么不使用$ .ajax来精确配置所有参数? – theshadowmonkey 2013-03-19 14:31:38
我是javascript/php的初学者,对ajax不太了解。 – user2108245 2013-03-19 14:36:38