跪求各位 php高手,为什么点击提交,数据库没有新增数据???
<!DOCTYPEHTML><html><head><metacharset="utf-8"><metaname="keywords"content="ajax表单提交,...
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="keywords" content="ajax表单提交,表单验证,jquery" />
<meta name="description" content="Helloweba提供在线演示HTML、CSS、jquery、PHP案例和示例" />
<title>演示:Ajax表单提交插件jquery form</title>
<link rel="stylesheet" type="text/css" href="http://www.helloweba.com/demo/css/main.css" />
<style type="text/css">
.demo{width:420px; margin:30px auto 0 auto}
.demo p{height:42px; line-height:42px}
.input{width:200px; line-height:24px; height:24px; padding:2px; border:1px solid #d3d3d3}
.btn{-webkit-border-radius: 3px;-moz-border-radius:3px;padding:6px 16px; margin-top:6px; cursor:pointer;background: #360;border: 1px solid #390;color:#fff}
#msg{margin-left:30px; line-height:24px; color:#f30}
#output{margin-top:10px}
</style>
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script type="text/javascript" src="jquery.form.min.js"></script>
<script type="text/javascript">
$(function(){
var options = {
beforeSubmit: showRequest,
success: showResponse,
resetForm: true,
dataType: 'json'
};
$('#my_form').submit(function() {
$(this).ajaxSubmit(options);
return false;
})
})
function showRequest(formData, jqForm, options) {
var uname = $("#uname").val();
if(uname==""){
$("#msg").html("姓名不能为空!");
return false;
}
var age = $("#age").val();
if(age==""){
$("#msg").html("年龄不能为空!");
return false;
}
alert("正在提交");
return true;
}
function showResponse(responseText, statusText) {
alert('提交成功');
}
</script>
</head>
<body>
<div id="main">
<div class="demo">
<form id="my_form" action="submit.php" method="post">
<p>姓名:<input type="text" name="uname" id="uname" class="input"></p>
<p>年龄:<input type="text" name="age" id="age" class="input" style="width:50px"></p>
<p style="margin-left:30px"><input type="submit" class="btn" value="提交"><span id="msg"></span></p>
</form>
<div id="output"></div>
</div>
</body>
</html>
这是我的submit.php文件
<?php
require 'config.php';
//新增用户
$query = "INSERT INTO user(uname,age)
VALUES('{$_POST['uname']}','{$_POST['age']}')";
mysql_query($query) or die('新增失败'.mysql_error());
mysql_close();
echo mysql_offected_rows();
$arr = $_POST;
$arr['msg']=1;
echo "提交成功";
//echo $_POST['uname'];
echo json_encode($arr);
?> 展开
<html>
<head>
<meta charset="utf-8">
<meta name="keywords" content="ajax表单提交,表单验证,jquery" />
<meta name="description" content="Helloweba提供在线演示HTML、CSS、jquery、PHP案例和示例" />
<title>演示:Ajax表单提交插件jquery form</title>
<link rel="stylesheet" type="text/css" href="http://www.helloweba.com/demo/css/main.css" />
<style type="text/css">
.demo{width:420px; margin:30px auto 0 auto}
.demo p{height:42px; line-height:42px}
.input{width:200px; line-height:24px; height:24px; padding:2px; border:1px solid #d3d3d3}
.btn{-webkit-border-radius: 3px;-moz-border-radius:3px;padding:6px 16px; margin-top:6px; cursor:pointer;background: #360;border: 1px solid #390;color:#fff}
#msg{margin-left:30px; line-height:24px; color:#f30}
#output{margin-top:10px}
</style>
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script type="text/javascript" src="jquery.form.min.js"></script>
<script type="text/javascript">
$(function(){
var options = {
beforeSubmit: showRequest,
success: showResponse,
resetForm: true,
dataType: 'json'
};
$('#my_form').submit(function() {
$(this).ajaxSubmit(options);
return false;
})
})
function showRequest(formData, jqForm, options) {
var uname = $("#uname").val();
if(uname==""){
$("#msg").html("姓名不能为空!");
return false;
}
var age = $("#age").val();
if(age==""){
$("#msg").html("年龄不能为空!");
return false;
}
alert("正在提交");
return true;
}
function showResponse(responseText, statusText) {
alert('提交成功');
}
</script>
</head>
<body>
<div id="main">
<div class="demo">
<form id="my_form" action="submit.php" method="post">
<p>姓名:<input type="text" name="uname" id="uname" class="input"></p>
<p>年龄:<input type="text" name="age" id="age" class="input" style="width:50px"></p>
<p style="margin-left:30px"><input type="submit" class="btn" value="提交"><span id="msg"></span></p>
</form>
<div id="output"></div>
</div>
</body>
</html>
这是我的submit.php文件
<?php
require 'config.php';
//新增用户
$query = "INSERT INTO user(uname,age)
VALUES('{$_POST['uname']}','{$_POST['age']}')";
mysql_query($query) or die('新增失败'.mysql_error());
mysql_close();
echo mysql_offected_rows();
$arr = $_POST;
$arr['msg']=1;
echo "提交成功";
//echo $_POST['uname'];
echo json_encode($arr);
?> 展开
2个回答
展开全部
分两步检查,1,写个测试文件,证明提交数据没问题,2
$query = "INSERT INTO user(uname,age)
VALUES('$_POST['uname']','$_POST['age']')";
$query = "INSERT INTO user(uname,age)
VALUES('$_POST['uname']','$_POST['age']')";
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询