我用AJAX提交保存数据,大家帮我看看有什么问题。

zzxap 2008-07-03 04:34:33

add.php
<script type="text/javascript">
var xmlHttp=false;
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlHttp=false;
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp=new XMLHttpRequest();
}
function CallServer()
{
var id=document.getElementById("id").value;
var bookname=document.getElementById("bookname").value;
var issuDate=document.getElementById("issuDate").value;
var price=document.getElementById("price").value;
var synopsis=document.getElementById("synopsis").value;
var Maker=document.getElementById("Maker").value;
var publisher=document.getElementById("publisher").value;

if (id!=""&bookname!=""&issuDate!=""&price!=""&synopsis!=""&Maker!=""&publisher!="")
{
var url ="save.php?id="+encodeURI(id); //,UTF-8下要用encodeURI
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=UpdateData;
xmlHttp.send(null);
}else
{
alert("please write all!");
}

}

function UpdateData()
{
if(xmlHttp.readyState<4)
{
span.innerHTML="loading...";
}

if(xmlHttp.readyState==4)
{
span.innerHTML=xmlHttp.responseText;
}
}
</script>

<form action="" method="POST">
id:<input type="text" name="id"><br>
bookname:<input type="text" name="bookname"><br>
issuDate:<input type="text" name="issuDate"><br>
price:<input type="text" name="price"><br>
synopsis:<input type="text" name="synopsis"><br>
Maker:<input type="text" name="Maker"><br>
publisher:<input type="text" name="publisher"><br>
<input type="button" name="submit" value="submit" onclick="CallServer()">
<form>

save.php

<?php

$host="localhost";
$user="root";
$pass="root";
$db="db1";

$id=empty($_POST['id'])? die ("id is nulll"):
mysql_escape_string($_POST['id']);
$bookname=empty($_POST['bookname'])? die ("bookname is nulll"):
mysql_escape_string($_POST['bookname']);

$issuDate=empty($_POST['issuDate'])? die ("issuDate is nulll"):
mysql_escape_string($_POST['issuDate']);

$price=empty($_POST['price'])? die ("price is nulll"):
mysql_escape_string($_POST['price']);
$synopsis=empty($_POST['synopsis'])? die ("synopsis is nulll"):
mysql_escape_string($_POST['synopsis']);

$Maker=empty($_POST['Maker'])? die ("Maker is nulll"):
mysql_escape_string($_POST['Maker']);

$publisher=empty($_POST['publisher'])? die ("publisher is nulll"):
mysql_escape_string($_POST['publisher']);

$connection=mysql_connect($host,$user,$pass) or die ("unable to connect");
mysql_select_db($db) or die ("unable to select database");
$query="insert into table1 (id,bookname,issuDate,price,synopsis,Maker,publisher)values('$id','$bookname','$issuDate','$price','$synopsis','$Maker','$publisher')";
$result=mysql_query($query) or die ("error in query:$query. ".mysql_error());
echo "success add ,mysql_insert_id()=".mysql_insert_id();
mysql_close($connection);
}
echo ("yes");
?>



按保存后没有反应,大家帮我看看有什么问题。


...全文
342 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzxap 2008-07-05
  • 打赏
  • 举报
回复
这不是AJAX框架吗??????
zzxap 2008-07-04
  • 打赏
  • 举报
回复
encodeURIComponent是什么意思
fxs_2008 2008-07-04
  • 打赏
  • 举报
回复
ajax要熟悉javascript

试试



var id=encodeURIComponent(document.getElementById("id").value));
其他类推
STR="id="+id+"&bookname="+bookname+"&issuDate="+issuDate+"&price="+price+"&synopsis="+synopsis+"&Maker="+Maker+"&publisher="+publisher;


zzxap 2008-07-04
  • 打赏
  • 举报
回复

改成这样还是不行啊
var id=encodeURI(encodeURI(document.getElementById("id").value));
var bookname=encodeURI(encodeURI(document.getElementById("bookname").value));
var issuDate=encodeURI(encodeURI(document.getElementById("issuDate").value));

var price=encodeURI(encodeURI(document.getElementById("price").value));
var synopsis=encodeURI(encodeURI(document.getElementById("synopsis").value));
var Maker=encodeURI(encodeURI(document.getElementById("Maker").value));
var publisher=encodeURI(encodeURI(document.getElementById("publisher").value));

var STR="id="+id+"&bookname="+bookname+"&issuDate="+issuDate+"&price="+price+"&synopsis="+synopsis+"&Maker="+Maker+"&publisher="+publisher;
xuzuning 2008-07-04
  • 打赏
  • 举报
回复
用 $_REQUEST 而不是 $_POST,$_GET
这样可以省缺许多麻烦
sdomain 2008-07-04
  • 打赏
  • 举报
回复
为什么不用AJAX框架呀?
pilishou 2008-07-03
  • 打赏
  • 举报
回复
get方式xmlHttp.open("GET",url,true);
接收页也全部改成$_GET[]
POST方式,其他不变
var url ="save.php?"
xmlHttp.open("POST",url,true);
xmlHttp.send("id"=id);
foolbirdflyfirst 2008-07-03
  • 打赏
  • 举报
回复


str = "id=" + "$_POST(id)";
str += "&bookname=$_POST(bookname)";
str += "&issuDate=$_POST(issuDate)";
str += "&price=$_POST(price)";
str += "&synopsis=$_POST(synopsis)";
str += "&Maker=$_POST(Maker)";
str += "&publisher=$_POST(publisher)";

改成以上思路正确,但也不能乱来.你在js代码里用$_POST?
请用js代码取表单值!

看看 http://www.openjs.com/articles/ajax_xmlhttp_using_post.php
ajax用成熟的js库吧,比如yui,jquery之类的,或者考虑自己封装一下。
fxs_2008 2008-07-03
  • 打赏
  • 举报
回复
其他没什么问题,核心的依然是多变量传值如何构造,我印象中角的是逗号隔开,你再查查

xmlHttp.send("id"=id,"bookname"=bookname,·······);

id不带引号的是变量
zzxap 2008-07-03
  • 打赏
  • 举报
回复
function CallServer()
{
var id=document.getElementById("id").value;

if (id!="")
{
var url ="save.php"; //UTF-8下要用encodeURI
str = "id=" + "$_POST(id)";
str += "&bookname=$_POST(bookname)";
str += "&issuDate=$_POST(issuDate)";
str += "&price=$_POST(price)";
str += "&synopsis=$_POST(synopsis)";
str += "&Maker=$_POST(Maker)";
str += "&publisher=$_POST(publisher)";


xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=UpdateData;

request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//post使用时的设定,必须加上。
request.send( str );

}else
{
alert("please write all!");
}

}

function UpdateData()
{
if(xmlHttp.readyState<4)
{
span.innerHTML="loading...";
}

if(xmlHttp.readyState==4)
{
span.innerHTML=xmlHttp.responseText;
}
if (request.readyState == 4)
{ // If the request is finished
if (request.status == 200) // If it was successful
alert(request.responseText);
document.getElementById("container").innerHTML = request.responseText; // Display the server's response
}
}


这样改行不行?
fxs_2008 2008-07-03
  • 打赏
  • 举报
回复
xmlHttp.send("id"=id,"bookname"=bookname,·······);

这POST多变量传值我记不清了,你再查查,看是不是上面的
fxs_2008 2008-07-03
  • 打赏
  • 举报
回复
xmlHttp.open("POST",url,true);
不能,你用的是get传值,却用Post方法提交,显然无值

要么你就直接用表单提交,不要调用AJAX了,

zzxap 2008-07-03
  • 打赏
  • 举报
回复
var url ="save.php?"
xmlHttp.open("POST",url,true);
xmlHttp.send("id"=id);
xmlHttp.send("bookname"=bookname);这样一个个send过去吗?

我在另外一个页面$id=$_POST['id']不能接收数据吗?
fxs_2008 2008-07-03
  • 打赏
  • 举报
回复
POST传值可能还要加header参数
zzxap 2008-07-03
  • 打赏
  • 举报
回复
xmlHttp.open("POST",url,true);
不知道这个能不能提交过去
fxs_2008 2008-07-03
  • 打赏
  • 举报
回复
你这个传值不是用的POST,用的是GET

两种办法,一是将用get方式

xmlHttp.open("GET",url,true);
接收页也全部改成$_GET[]


第二种办法是用POST方式,其他不变
试试
var url ="save.php?"
xmlHttp.open("POST",url,true);
xmlHttp.send("id"=id);

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧