How can I assign different actions for different submit buttons in the same html form?

I am trying to assign different actions to the same html form according to different submit buttons.

Can I do something like this?

<FORM>
------
<INPUT type="submit" value="DoSomething" action="DoSomething.pl" method="POST">
<INPUT type="submit" value="DoSomethingElse" action="DoSomethingElse.pl" method="POST">
<FORM/> 
+4
source share
3 answers

No. There is only one form action( actionwhich is a property of the form , not a submit button).

The purpose of an action can do different things based on the values ​​in the form. So you can start calling the submit buttons.

Learn HTML before you even think about writing and deploying a CGI script.

<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.

, . , , , script, , IMHO, . , - script ( , curl wget .

, 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>

script :

#!/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 { }
+11

- :

HTML5, formaction. input button 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>
+13

Ajax , ajax, php script, , , :

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();

// html,

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

//script,

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 files should contain some variables that will store the values ​​sent by the dataVar parameter as follows:

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

An example that I used can be found here: https://www.youtube.com/watch?v=woNQ2MA_0XU .

-1
source

All Articles