如何用xmlhttprequest, 把用户输入的数据 post 到cgi程序中. 接收数据端的页面是用 cgi写的,
<html>
<head>
<script type="text/javascript">
var xmlhttp;
var params = "name=jese&易做图=womanz";
function postXMLDoc(url)
{
document.write("<i>" + url + " </i><br/>");
document.write("<b>" + params + " </b><br/>");
xmlhttp=null;
if (window.XMLHttpRequest)
{// code for IE7, Firefox, Opera, etc.
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
/*xmlhttp.onreadystatechange=state_Change;*/
xmlhttp.open("POST", url, false);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
/*xmlhttp.setRequestHeader("Content-length", params.length); */
xmlhttp.send(params);
}
else
{
alert("Your browser does not support XMLHTTP.");
}
}
function state_Change()
{
if (xmlhttp.readyState==4)
{// 4 = "loaded"
document.write("<b>" + " responseText "+ " </b><br/>");
if (xmlhttp.status==200)
{// 200 = "OK"
document.write("OK OK OK OK ");
}
else
{
alert("Problem retrieving XML data:" + xmlhttp.statusText);
}
}
}
</script>
</head>
<body>
<h2>Using the HttpRequest Object</h2>
<form>
Email: <input type="text" name="Email" size="30"><br/>
Name: <input type="text" name="Name" size="30"><br/>
<button onclick="postXMLDoc('/cgi-bin/gbook.cgi')">post date</button>
</form>
</body>
</html>
服务器上有 cgi-bin/gbook.cgi 这个文件, 可以显示,
但是怎样把用户输入的 email和name, 传给 gbook.cgi ?? 谢谢.
追问:非常谢谢你,
怎样把 Email和 name 放到 javascript 中, 然后再 send ? 谢谢
答案:给表单加上ID方便下面取值
Email: <input type="text" name="Email" size="30" id="Email"><br/>
Name: <input type="text" name="Name" size="30" id="Name"><br/>
//把对应的应变放到函数中
function postXMLDoc(url){
var xmlhttp,Name=document.getElementById("Name").value,Email=document.getElementById("Email").value;
var params = "name="+Name+"&Email="+Email+"&易做图=womanz";
//把下面的两句去掉
// document.write("<i>" + url + " </i><br/>");
// document.write("<b>" + params + " </b><br/>
if (window.XMLHttpRequest)
{// code for IE7, Firefox, Opera, etc.
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
/*xmlhttp.onreadystatechange=state_Change;*/
xmlhttp.open("POST", url, false);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
/*xmlhttp.setRequestHeader("Content-length", params.length); */
xmlhttp.send(params);
}
else
{
alert("Your browser does not support XMLHTTP.");
}
}
function state_Change()
{
if (xmlhttp.readyState==4)
{// 4 = "loaded"
document.write("<b>" + " responseText "+ " </b><br/>");
if (xmlhttp.status==200)
{// 200 = "OK"
document.write("OK OK OK OK ");
}
else
{
alert("Problem retrieving XML data:" + xmlhttp.statusText);
}
}
}
上一个:perl 的cgi脚本无法读取标准输入
下一个:如何限制php cgi.exe的进程个数呢?