2

第一次发帖:)

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

谢谢 !

4

1 回答 1

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;
}
于 2018-01-26T05:02:14.920 回答