2010-10-22 182 views

回答

11

号公报形式只有一个actionaction作为form的属性,而不是提交按钮)。

动作的目标可以根据表单中的值做不同的事情。所以,你可能想要开始命名你的提交按钮。

在您考虑编写和部署CGI脚本之前,请先学习HTML。

<form method="POST" action="/cgi-bin/script"> 
<input type="submit" name="action" value="DoSomething"> 
<input type="submit" name="action" value="DoSomethingElse"> 
</form> 

还要注意选择基于提交按钮的值的行动是一个失败的策略,如果你希望国际化的应用程序,因为一个提交按钮的value是什么UA展示给人类。

因此,script应该根据其他输入元素的值决定如何处理。

例如,CGI::Application查看run_mode参数。

或者,您可以使用不同的名称作为您的提交按钮,如亚历克所示。在这种情况下,您需要通过查看传递给脚本的参数名称来检查哪个提交按钮被按下了,恕我直言,这会使调度变得更加麻烦。这也意味着它可能是有人将值传递所有提交按钮,您的脚本(不通过用户界面,但通过curlwget或类似的计划。

例如,给出的HTML

<form method="POST" action="/cgi-bin/script"> 
<input type="submit" name="submit_left" value="Go Left"> 
<input type="submit" name="submit_right" value="Go Right"> 
</form> 

这里是你的脚本可以如何处理表单提交:

#!/usr/bin/perl 

use strict; use warnings; 

use CGI::Simple; 

my $cgi = CGI::Simple->new; 

my %dispatch = (
    left => \&handle_left, 
    right => \&handle_right, 
); 

my @actions = grep s/^action_(right|left)\z/$1/, $cgi->param; 

my $handler = \&handle_invalid_action; 

if (@actions == 1) { 
    my ($action) = @actions; 
    if (exists $dispatch{ $action }) { 
     $handler = $dispatch{ $action }; 
    } 
} 
else { 
    $handler = \&handle_too_many_actions; 
} 

$handler->($cgi); 

sub handle_left { } 
sub handle_right { } 
sub handle_invalid_action { } 

# because it may indicate someone trying to abuse your script 
sub handle_too_many_actions { } 
+0

在上面的示例代码中,两个提交按钮是冗余的。它们两个都将执行相同的操作。正确? – Jean 2010-10-22 18:22:28

+3

@alertjean,这取决于脚本。提交按钮具有不同的值,因此脚本可以确定哪个按钮被点击,并根据该按钮做不同的事情。 – cjm 2010-10-22 19:35:19

+0

通常使用'name'属性可以区分不同的动作。例如。 ''和''。这样你可以匹配简化的动作名称而不是实际的文本。这也是您使用多语言网站时唯一的方式,因为价值永远不会相同。 – Alec 2010-10-22 22:26:52

13

万一别人发现这个帖子:

我如果您使用的是HTML5,现在可以使用formaction属性更容易了。此属性适用于inputbutton元素type="submit",并强制该表单提交到点击元素的formaction属性中指定的位置。

然后只有这个属性的缺点是它不被Internet Explorer 9和更低版本支持,但是这个限制可以通过使用一点JavaScript轻松解决。

例子:

<form method="post" action="go_default"> 
    <input type="submit" value="Go Left" formaction="go_left" /> 
    <input type="submit" value="Go Right" formaction="go_right" /> 
</form> 

对于IE 9,下:

<script type="text/javascript"> 
    $(function() { 
     var $submit = $('form [type="submit"][formaction]'); 

     $submit.click(function() { 
      var $this = $(this), 
       action = $this.prop('formaction'), 
       $form = $this.closest('form'); 

      $form.prop('action', action).submit(); 
     }); 
    }); 
</script> 
-1

你也可以使用Ajax对于这一点,每一个按钮分配调用一个Ajax功能它自己PHP脚本,你甚至不需要刷新页面或重定向,就像我尝试过的这个例子:

HTML: 
<input type="submit" value="Make other thing" onclick="ajax_post1();"/> 
<input type="submit" value="Make something" onclick="ajax_post2();"/> 

<div id="script1Response"></div> 
<div id="script2Response"></div> 

Javascript函数:

//第一功能

function ajax_post1(){ 
var hr = new XMLHttpRequest(); 

//从您要使用

var v1=document.getElementbyId("element1").value; 
var v2=document.getElementbyId("element2").value; 

//脚本的HTML输入元素取值是将处理数据

var url="php_script1.php"; 

//将包含为PHP脚本

var dataVar="var1="+v1+"&var2="+v2; 
hr.open("POST", url, true); 
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

的信息的变量//进入onreadystatechange事件的XMLHttpRequest对象

hr.onreadystatechange = function() { 
    if(hr.readyState == 4 && hr.status == 200) { 
    var script_response = hr.responseText; 
    document.getElementById("script1Response").innerHTML = script_response; 
    } 
    } 

//发送数据到php_script1.php

hr.send(dataVar); // Actually execute the request 
document.getElementById("script1Response").innerHTML = "processing..."; 
} 

//第二功能

function ajax_post2(){ 

var v1=document.getElementbyId("element1").value; 
var v2=document.getElementbyId("element2").value; 
var url="php_script2.php"; 
var dataVar="var1="+v1+"&var2="+v2; 
var hr = new XMLHttpRequest(); 
hr.open("POST", url, true); 
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

hr.onreadystatechange = function() { 
    if(hr.readyState == 4 && hr.status == 200) { 
    var script_response = hr.responseText; 
    document.getElementById("script2Response").innerHTML = script_response; 
    } 
    } 

hr.send(dataVar); 
document.getElementById("script2Response").innerHTML = "processing..."; 

} 

PHP文件必须包含一些变量,将存储由dataVar参数这样发送的值:

$var1_=$_POST['var1']; //the var1 from the dataVar parameter 
$var2_=$_POST['var2']; //the var2 from the dataVar parameter 

我使用的例子可以在这里找到: https://www.youtube.com/watch?v=woNQ2MA_0XU

相关问题