Home  >  Article  >  Backend Development  >  求助:ajax获取并以post方式发送表单数据

求助:ajax获取并以post方式发送表单数据

WBOY
WBOYOriginal
2016-06-23 14:38:111182browse

本帖最后由 u012296415 于 2013-11-26 11:04:16 编辑


 
 
 you can add a data into this table
 
 <script> <br /> function loadXMLDoc () <br /> { <br /> var xmlhttp; <br /> xmlhttp=new XMLHttpRequest (); <br /> xmlhttp.open("POST","insert.php",true); <br /> xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); <br /> xmlhttp.send("name&sex&birth"); <br /> } <br /> </script>
 
 
 name: 
 sex: 
 birth: 
 
 
 
 
以上是网页代码,求助如何获取表单中的数据并用xmlhttp.send发送给insert.php
 新人初学,还请多多关照

回复讨论(解决方案)

insert.php的代码是这样的
   error_reporting(E_ALL ^E_DEPRECATED);
 $con = mysql_connect("localhost:/tmp/mysql.sock","chris","6661573");
 if (!$con)
     echo "shibai";
   else echo "connect mysql success";
 mysql_select_db("birth", $con);
 $sql="insert shengri (name,sex,birth) values ('$_POST[name]','$_POST[sex] ','$_POST[birth]')";
 if (!mysql_query($sql,$con))
   {
   die('Error: ' . mysql_error());
   }
 echo "1 record added";
 mysql_close($con)
 ?>
 求大神们给看看 

把你前台的页面换成下面的

 


 
 you can add a data into this table
 
 <script> <br /> function loadXMLDoc () <br /> { <br /> var xmlhttp; <br /> var name = document.getElementById("name").value; <br /> var sex = document.getElementById("sex").value; <br /> var birth = document.getElementById("birth").value; <br /> <br /> xmlhttp=new XMLHttpRequest (); <br /> xmlhttp.onreadystatechange = function() <br /> { <br /> if (xmlhttp.readyState == 4 && xmlhttp.responseText) <br /> { <br /> alert(xmlhttp.responseText); <br /> } <br /> } <br /> xmlhttp.open("POST","./insert.php",true); <br /> xmlhttp.setRequestHeader('content-type','application/x-www-form-urlencoded'); <br /> xmlhttp.send("name="+name+"&sex="+sex+"&birth="+birth); <br /> } <br /> </script>
 
 
 name: 
 sex: 
 birth: 
 
 
 

非常感谢楼上的回答,很管用。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn