第一次发帖:)
我正在使用Ruby进行练习,我对CGI方法感到有些困惑。
我使用下面的HTML代码发送表单的输入。
来自HTML的位
<form action="afficher.cgi" method="post">
<label> Votre prenom </label> : <input type="text" name="prenom"/> <br>
<label> Votre nom </label> : <input type="text" name="nom"/> <br>
<label> Votre nom </label> : <input type="text" name="age"/> <br>
<input type="submit" value="Valider" />
</form>
当我提交表单时,它会转到.cgi页面(执行脚本)并运行。
我想知道是否可以将信息发送到.cgi并刷新.rhtml而不是转到.cgi并被卡在那里。
我试图在重定向到.rhtml的.cgi中找到一个方法或一些东西,但没有任何线索。 .rhtml文档中是否有解决方案?
谢谢!
答案 0 :(得分:1)
你可能不得不使用javascript / ajax。
在form
标记中,您应指定onsubmit
字段的功能,而不是在action
中指定目的地。
<form onsubmit="return submitForm(this)" id="form1" action="" method="post">
然后,在你的javascript中,
function submitForm()
{
var xmlhttp= window.XMLHttpRequest ?
new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
alert(xmlhttp.responseText); // Here is the response
}
var formData = new FormData( document.getElementById("form1") );
xmlhttp.open("POST","afficher.cgi", true);
xmlhttp.send(formData);
return false;
}