使用CGI运行脚本

时间:2018-01-26 00:54:32

标签: html ruby cgi webrick rhtml

第一次发帖:)

我正在使用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文档中是否有解决方案?

谢谢!

1 个答案:

答案 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;
}