如题。。第一次写ajax的post提交表单,感觉和get差别好大, 模仿get 写了一下,各位大大帮忙看一下是怎么回事。。万分感谢。
orm的button
<!-- Button -->
<div class="controls">
<button type="button" class="btn btn-success" id="xinxiformbuttom" onclick="startXMLHttp()">提交信息</button>
</div>
</div><script type="text/javascript">
var xmlHttp=null;
function createXMLHttp(){
if(window.XMLHttpRequest){ // Mozilla 浏览器
xmlHttp = new XMLHttpRequest();
}else if (window.ActiveXObject){ // IE 浏览器
try{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
return xmlHttp;
}
//建立主过程
function startXMLHttp(){
xmlHttp=createXMLHttp();//建立xmlHttp 对象
if (xmlHttp==null)
{
alert ("浏览器不支持 HTTP 传输")
return
}
var timepoin = document.getElementById("time").value
var codebelong = document.getElementById("codebelong").value
var richeng = document.getElementById("dateschedu").value
var description = document.getElementById("detail").value
var codeup = document.getElementById("code").value
var send_string="name="+document.getElementById("name").value;
send_string = send_string +"&timepoin"+timepoin+"&codebelong"+codebelong+"&richeng"+richeng+"&description"+description+"&codeup"+codeup
send_string= encodeURI(send_string)
// alert(document.getElementById("text").value);
// return;
xmlHttp.onreadyStatechange =dodo; //xmlHttp下的onreadystatechange方法控制传送过程
xmlHttp.open("Post","controlpage/upcodefile.php",true);//传送方式 读取的页面 异步与否
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); // xmlHttp.setRequestHeader("cache-control","no-cache");
xmlHttp.send(send_string); //发送
}
function dodo(){
if(xmlHttp.readyState==4){ // xmlHttp下的readystate方法 4表示传送完毕
if(xmlHttp.status==200){ // xmlHttp的status方法读取状态(服务器HTTP状态码) 200对应OK 404对应Not Found(未找到)等
document.getElementById("detail").innerHTML=xmlHttp.responseText; //xmlHttp的responseText方法 得到读取页数据
}
}
}
</script>
想要显示在detail这个input里
orm的button
<!-- Button -->
<div class="controls">
<button type="button" class="btn btn-success" id="xinxiformbuttom" onclick="startXMLHttp()">提交信息</button>
</div>
</div><script type="text/javascript">
var xmlHttp=null;
function createXMLHttp(){
if(window.XMLHttpRequest){ // Mozilla 浏览器
xmlHttp = new XMLHttpRequest();
}else if (window.ActiveXObject){ // IE 浏览器
try{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
return xmlHttp;
}
//建立主过程
function startXMLHttp(){
xmlHttp=createXMLHttp();//建立xmlHttp 对象
if (xmlHttp==null)
{
alert ("浏览器不支持 HTTP 传输")
return
}
var timepoin = document.getElementById("time").value
var codebelong = document.getElementById("codebelong").value
var richeng = document.getElementById("dateschedu").value
var description = document.getElementById("detail").value
var codeup = document.getElementById("code").value
var send_string="name="+document.getElementById("name").value;
send_string = send_string +"&timepoin"+timepoin+"&codebelong"+codebelong+"&richeng"+richeng+"&description"+description+"&codeup"+codeup
send_string= encodeURI(send_string)
// alert(document.getElementById("text").value);
// return;
xmlHttp.onreadyStatechange =dodo; //xmlHttp下的onreadystatechange方法控制传送过程
xmlHttp.open("Post","controlpage/upcodefile.php",true);//传送方式 读取的页面 异步与否
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); // xmlHttp.setRequestHeader("cache-control","no-cache");
xmlHttp.send(send_string); //发送
}
function dodo(){
if(xmlHttp.readyState==4){ // xmlHttp下的readystate方法 4表示传送完毕
if(xmlHttp.status==200){ // xmlHttp的status方法读取状态(服务器HTTP状态码) 200对应OK 404对应Not Found(未找到)等
document.getElementById("detail").innerHTML=xmlHttp.responseText; //xmlHttp的responseText方法 得到读取页数据
}
}
}
</script>
想要显示在detail这个input里
<?php
$name = $_POST['name'];
$timepoin = $_POST['timepoin'];
$codebelong = $_POST['codebelong'];
$richeng = $_POST['richeng'];
$description = $_POST['description'];
$codeup = $_POST['codeup']; echo $description;?>
你不觉得少了点什么吗?
send_string = send_string +"&timepoin="+timepoin+"&codebelong="+codebelong+"&richeng="+richeng+"&description="+description+"&codeup="+codeup另外,encodeURI 要对每个值做处理,而不是对 send_string 整体
&timepoin="+timepoin参数少写了=