ajax(josn)+php+mysql疑问,求高人指点

xx.html页面有2个文本框,点击按钮,把2个文本框的值,传给action.php页面,action.php页面处理,具体业务是插入数据库,然后查询数据库,把查询结果放... xx.html页面有2个文本框,点击按钮,把2个文本框的值,传给action.php页面,action.php页面处理,具体业务是插入数据库,然后查询数据库,把查询结果放在数组$new_array()里面,然后用Services_JSON,传给xx.html页面。但是每次点按钮,都会先向数据库里面插入一条空的记录,然后再插入根据xx.html页面传过来的值的记录。。。
xx.html页面
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#button").click(function(){
var user=$("#user").val();
var contant=$("#contant").val();
$.ajax({
type:"GET",
url:"action.php",
data: "action=liuyanban&user="+user+"&r-------"+Math.round(Math.random()*1000)+"&contant="+contant+"&r-------"+Math.round(Math.random()*1000),
dataType:"json",
success:function(){
$.getJSON("action.php", function(json){
for(var i = 0; i < json.length; i++)
{
alert("id:" + json[i].id);
alert("user:" + json[i].user);
alert("contant:" + json[i].contant);
alert("time:" + json[i].time);
}
});
}
});
});
});
</script>
</head>
<body>
<input id="user" type="text"><br><br>
<input id="contant" type="text"> <input id="button" type="button" value="留言">
</body>
</html>

action.php页面
<?php
require('JSON.php');
if($_GET['action']=='liuyanban')
$user = $_GET['user'];
$contant = $_GET['contant'];
$new_array=array();
$con = mysql_connect("localhost","root","admin");
mysql_select_db("test", $con);
$result = mysql_query("SELECT * FROM lyb");
while($row = mysql_fetch_assoc($result))
{
$new_array[]=$row;
}
mysql_query("INSERT INTO `test`.`lyb` (`id` ,`user` ,`contant` ,`time`)VALUES (NULL , '".$user."', '".$contant."', '2011-12-20')");
mysql_close($con);
$json = new Services_JSON();
echo $json->encode($new_array);
?>
展开
 我来答
百度网友f20790b46
2011-12-20 · TA获得超过216个赞
知道小有建树答主
回答量:359
采纳率:0%
帮助的人:252万
展开全部
$.getJSON("action.php", function(json){
for(var i = 0; i < json.length; i++)
{
alert("id:" + json[i].id);
alert("user:" + json[i].user);
alert("contant:" + json[i].contant);
alert("time:" + json[i].time);
}
});

你这样,不是又执行一遍 action.php 么,没有传值,当然插入空记录了。。
更多追问追答
追问
请问如何修改啊???
追答
去掉啊 ,读取ajax返回的 data不就可以了么~~~
贰两毛尖
2011-12-21 · TA获得超过862个赞
知道小有建树答主
回答量:1102
采纳率:0%
帮助的人:898万
展开全部
一楼正解
帮你改了一下js

$(document).ready(function () {
$("#button").click(function () {
var user = $("#user").val();
var contant = $("#contant").val();
$.ajax({
type : "GET",
url : "action.php",
data : "action=liuyanban&user=" + user + "&r-------" + Math.round(Math.random() * 1000) + "&contant=" + contant + "&r-------" + Math.round(Math.random() * 1000),
dataType : "json",
success : function (json) {
for (var i = 0; i < json.length; i++) {
alert("id:" + json[i].id);
alert("user:" + json[i].user);
alert("contant:" + json[i].contant);
alert("time:" + json[i].time);
}
}
});
});
});
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
Lw0rv3
2011-12-22
知道答主
回答量:13
采纳率:0%
帮助的人:4.9万
展开全部
$(document).ready(function () {
$("#button").click(function () {
var user = $("#user").val();
var contant = $("#contant").val();
$.ajax({
type : "GET",
url : "action.php",
data : "action=liuyanban&user=" + user + "&r-------" + Math.round(Math.random() * 1000) + "&contant=" + contant + "&r-------" + Math.round(Math.random() * 1000),
dataType : "json",
success : function (json) {
for (var i = 0; i < json.length; i++) {
alert("id:" + json[i].id);
alert("user:" + json[i].user);
alert("contant:" + json[i].contant);
alert("time:" + json[i].time);
}
}
});
});
});
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式