【问题标题】:Get the name of a html submit button via CGI通过 CGI 获取 html 提交按钮的名称
【发布时间】:2013-09-17 08:06:03
【问题描述】:

我在 CGI 脚本(C++ 中)中有两个提交按钮。一个 (value=Submit) 只是保存表单数据。另一个(value=Save and Reboot)应该以相同的方式保存表单数据,然后重新启动。

基本上,我想这样做:

 if(method == "POST")
 {
   //element 1 saved

   //element 2 saved

   //etc etc

   if(second button is pushed)
     //handle reboot code
  }

但是我怎么知道是哪个按钮被按下了呢?我看到了this question,但它在 php 中。我正在使用 C++ CGI 脚本。我一直在阅读CGICC documentation,但找不到任何关于确定按下哪个按钮的信息。

需要明确的是,两个按钮本质上都会做同样的事情(“POST”),但我需要能够识别按钮,以便在必要时重新启动。

【问题讨论】:

    标签: c++ html cgi


    【解决方案1】:

    假设您有两个按钮,一个名为“提交”,一个名为“保存并重新启动”:

    <form method="POST" action="">
        <input type="submit" name="Submit" />
        <input type="submit"  name="Save and reboot" />
    </form>
    

    现在,只需检查 POST 数据中是否设置了具有适当名称的值:

    form_iterator fsubmit = formData.getElement("Submit");
    if( !fsubmit->isEmpty() && fsubmit != (*formData).end()) {
       // The sumbit is pressed
    }
    
    form_iterator fsaveandreboot = formData.getElement("Save and reboot");
    if( !fsaveandreboot->isEmpty() && fsaveandreboot != (*formData).end()) {
       // The save and reboot is pressed
    }
    

    一旦你明白没有被点击的按钮不会包含在 POST 数据中,这就很容易写了。

    【讨论】:

    • 好的,这很有意义。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 2011-10-17
    • 1970-01-01
    • 2021-04-09
    相关资源
    最近更新 更多