客户端代码:
<html>
 <head>
  <title>Information</title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  <script type="text/javascript">
var xmlHttp ;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP") ;
}else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest() ;
}
}
function check(){
createXMLHttpRequest() ;
var name=document.getElementById("name").value ;
var author=document.getElementById("author").value ;
var pl=document.getElementById("publisher") ;
var publisher=pl.options[pl.selectedIndex].text ;
var isbn=document.getElementById("isbn").value ;
var tp=document.getElementById("type") ;
var publisher=tp.options[tp.selectedIndex].text ;
var price=document.getElementById("price").value ; var str="name="+name+"&author="+author+"&publisher="+publisher+"&isbn="+isbn+"&type="+type+"&price="+price ;
var url="addsend.php" ;
xmlHttp.open("POST" , url , true) ;
xmlHttp.onreadystatechange=callback ;
xmlHttp.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded") ;
xmlHttp.send(str) ; }
function callback(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
var str1=xmlHttp.responseText ;
if(str1=="1"){
alert("图书数据成功添加") ;
window.location.reload() ;
}
}
}
}
  </script>
 </head> <body>
  <h2 align="center">添加图书</h2>
  <p align="center"><a href="index.php">显示信息</a>|<a href="update.php">数据修改</a>|<a href="del.php">数据删除</a>
  </p> <form name="myform">
<table align="center">
<tr><td>图书名称:</td><td><input type="text" id="name"></td></tr>
<tr><td>图书作者:</td><td><input type="text" id="author"></td></tr>
<tr><td>出版社:</td><td><select id="publisher"><option>清华大学出版社</option><option selected>人民邮电出版社</option><option>电子工业出版社</option><option>河南出版社</option></select></td></tr>
<tr><td>ISBN号:</td><td><input type="text" id="isbn"></td></tr>
<tr><td>图书类型:</td><td><select id="type"><option selected>计算机类图书</option><option>经济类图书</option><option>财经类图书</option><option>哲学类图书</option><option>旅游类图书</option></select></td></tr>
<tr><td>图书价格:</td><td><input type="text" id="price"></td></tr>
<tr><td align="center" colspan="2"><input type="button" value="OK" onclick="return check()">&nbsp;&nbsp;&nbsp;<input type="reset" value="重置"></td></tr>
</table>
 </form>
 </body>
</html>服务器:
<?php
header("Content-Type:text/html ; chareset:gb2312") ;
$name=$_POST["name"] ;
$author=$_POST["author"] ;
$publisher=$_POST["publisher"] ;
$isbn=$_POST["isbn"] ;
$type=$_POST["type"] ;
$price=$_POST["price"] ; //$name=iconv("utf-8" , "gb2312//IGNORE" , $_POST["name"]) ;
//$author=iconv("utf-8" , "gb2312//IGNORE" , $_POST["author"]) ;
//$publisher=iconv("utf-8" , "gb2312//IGNORE" , $_POST["publisher"]) ;
//$isbn=iconv("utf-8" , "gb2312//IGNORE" , $_POST["isbn"]) ;
//$type=iconv("utf-8" , "gb2312//IGNORE" , $_POST["type"]) ;
//$price=iconv("utf-8" , "gb2312//IGNORE" , $price) ;
$conn=mysql_connect("localhost" , "root" , "root") or die("Could not link".mysql_error()) ;
mysql_select_db("student" , $conn) ;
mysql_query("set names 'gb2312'") ;
$result=mysql_query("insert into book(name , author , publisher , isbn , type , price) values('".$name."' , '".$author."' , '".$publisher."' , '".$isbn."' , '".$type."' , '".$price."')") ; var_dump($name) ;
var_dump($author) ;
var_dump($publisher) ;
var_dump($isbn) ;
var_dump($type) ;
var_dump($price) ; if($result){
echo "1" ;
}
mysql_close() ;
?>数据库建立了,但点击按钮的时候,一点反应液没有,php运行环境:phpstudy。

解决方案 »

  1.   

    我这里成功了, 不清楚你那里怎么了.<html>
     <head>
      <title>Information</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
      <script type="text/javascript">
    var xmlHttp ;
    function createXMLHttpRequest(){
    if(window.ActiveXObject){
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP") ;
    }else if(window.XMLHttpRequest){
    xmlHttp=new XMLHttpRequest() ;
    }
    }
    function check(){
    createXMLHttpRequest() ;
    var name=document.getElementById("name").value ;
    var author=document.getElementById("author").value ;
    var pl=document.getElementById("publisher") ;
    var publisher=pl.options[pl.selectedIndex].text ;
    var isbn=document.getElementById("isbn").value ;
    var tp=document.getElementById("type") ;
    var publisher=tp.options[tp.selectedIndex].text ;
    var price=document.getElementById("price").value ;var str="name="+name+"&author="+author+"&publisher="+publisher+"&isbn="+isbn+"&type="+type+"&price="+price ;
    var url="addsend.php" ;
    xmlHttp.open("POST" , url , true) ;
    xmlHttp.onreadystatechange=callback ;
    xmlHttp.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded") ;
    xmlHttp.send(str) ;
    return false;
    }
    function callback(){
    if(xmlHttp.readyState==4){
    if(xmlHttp.status==200){
    var str1=xmlHttp.responseText ;
    alert(str1);
    if(str1=="1"){
    alert("图书数据成功添加") ;
    window.location.reload() ;
    }
    }
    }
    }
      </script>
     </head> <body>
      <h2 align="center">添加图书</h2>
      <p align="center"><a href="index.php">显示信息</a>|<a href="update.php">数据修改</a>|<a href="del.php">数据删除</a>
      </p> <form name="myform">
    <table align="center">
    <tr><td>图书名称:</td><td><input type="text" id="name"></td></tr>
    <tr><td>图书作者:</td><td><input type="text" id="author"></td></tr>
    <tr><td>出版社:</td><td><select id="publisher"><option>清华大学出版社</option><option selected>人民邮电出版社</option><option>电子工业出版社</option><option>河南出版社</option></select></td></tr>
    <tr><td>ISBN号:</td><td><input type="text" id="isbn"></td></tr>
    <tr><td>图书类型:</td><td><select id="type"><option selected>计算机类图书</option><option>经济类图书</option><option>财经类图书</option><option>哲学类图书</option><option>旅游类图书</option></select></td></tr>
    <tr><td>图书价格:</td><td><input type="text" id="price"></td></tr>
    <tr><td align="center" colspan="2"><input type="button" value="OK" onclick="var ret = check();alert(ret);">&nbsp;&nbsp;&nbsp;<input type="reset" value="重置"></td></tr>
    </table>
     </form>
     </body>
    </html>
    [User:root Time:16:00:26 Path:/home/apache/web]$ cat addsend.php 
    <?php
    echo "1";
    ?>
      

  2.   

    问题1: str后面中type没有取到值,请仔细看var name=document.getElementById("name").value ;
    var author=document.getElementById("author").value ;
    var pl=document.getElementById("publisher") ;
    var publisher=pl.options[pl.selectedIndex].text ;var isbn=document.getElementById("isbn").value ;
    var tp=document.getElementById("type") ;var publisher=tp.options[tp.selectedIndex].text ;var price=document.getElementById("price").value ;var str="name="+name+"&author="+author+"&publisher="+publisher+"&isbn="+isbn+"&type="+type+"&price="+price ;问题2:把addsend.php里的var_dump()调试去掉,并且看下数据库连接有没有错
      

  3.   

    有两个publisher  ,,应该把最后那个var publisher改成 var type
      

  4.   

    在360浏览器中的运行效果为:
    Notice: Undefined index: name in D:\www\Ajax\2\addsend.php on line 3Notice: Undefined index: author in D:\www\Ajax\2\addsend.php on line 4Notice: Undefined index: publisher in D:\www\Ajax\2\addsend.php on line 5Notice: Undefined index: isbn in D:\www\Ajax\2\addsend.php on line 6Notice: Undefined index: type in D:\www\Ajax\2\addsend.php on line 7Notice: Undefined index: price in D:\www\Ajax\2\addsend.php on line 8
    但是我找了一些资料,感觉没错呀!纳闷中...希望高手相助,在下将不胜感激。
    我的运行环境为phpstudy、win7系统自带的IE浏览器。
      

  5.   

    你这个可以关闭PHP的DISPLAY ERROR就可以了。
      

  6.   

    addsend.php页面没有接收到post参数:var str="name="+name+"&author="+author+"&publisher="+publisher+"&isbn="+isbn+"&type="+type+"&price="+price ;
    var url="addsend.php" ;
    //改为:
    var url="addsend.php?"+str ;
      

  7.   

    这个帖子可能对你有帮助:
    http://blog.csdn.net/fgdfgasd/article/details/6947919