日期:2014-05-16  浏览次数:20423 次

求助:ajax获取并以post方式发送表单数据
本帖最后由 u012296415 于 2013-11-26 11:04:16 编辑
<html>
 <head>
 </br>
 you can add a data into this table
 </br>
 <script type="text/javascript">
 function loadXMLDoc ()
 {
 var xmlhttp;
 xmlhttp=new XMLHttpRequest ();
 xmlhttp.open("POST","insert.php",true);
 xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 xmlhttp.send("name&sex&birth");
 }
 </script>
 </head>
 <body>
 name: <input type="text" name="name" />
 sex: <input type="text" name="sex" />
 birth: <input type="date" name="birth" />
 <button type="button" onclick="loadXMLDoc ()">submit </button>
 </body>
 </html>
 
以上是网页代码,求助如何获取表单中的数据并用xmlhttp.send发送给insert.php
 新人初学,还请多多关照
php ajax

------解决方案--------------------
把你前台的页面换成下面的
<html>
 <head>
 </br>
 you can add a data into this table
 </br>
 <script type="text/javascript">
 function loadXMLDoc ()
 {
 var xmlhttp;
 var name = document.getElementById("name").value;
 var sex = document.getElementById("sex").value;
 var birth = document.getElementById("birth").value;

 xmlhttp=new XMLHttpRequest ();
 xmlhttp.onreadystatechange = function()
 {
if (xmlhttp.readyState == 4 && xmlhttp.responseText)
{
alert(xmlhttp.responseText);
}
 }
 xmlhttp.open("POST","./insert.php",true);
 xmlhttp.setRequestHeader('content-type','application/x-www-form-urlencoded');
 xmlhttp.send("name="+name+"&sex="+sex+"&birth="+birth);
 }
 </script>
 </head>
 <body>
 name: <input type="text" name="name" id="name" />
 sex: <input type="text" name="sex" id="sex"/>
 birth: <input type="date" name="birth" id="birth"/>
 <button type="button" onclick="loadXMLDoc ()">submit </button>
 </body>
 </html>