【问题标题】:Using CGI to run a script使用 CGI 运行脚本
【发布时间】:2018-01-26 00:54:32
【问题描述】:

第一次发帖:)

我正在用 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 并卡在那里。

我试图在 .cgi 中找到一种方法或一些东西来重定向到 .rhtml,但没有任何线索。 .rhtml 文档中有解决方案吗?

谢谢!

【问题讨论】:

    标签: html ruby cgi webrick rhtml


    【解决方案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;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-30
      • 2015-05-27
      • 2016-10-31
      • 2013-03-11
      • 2011-12-17
      • 1970-01-01
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多