简体   繁体   中英

how to Link html form and anchor tag

I am new to HTML. I want the following to be done in html.

if(link is clicked) {
    process one form tag
}
else {
    some other form tag
}

       this is my form tag . 
        <form name="input" action="abc.pl" method="get" id="sel">
        <input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
         <input type="checkbox" name="vehicle" value="Car">I have a car 
        </form>

I want a link such that if i click on the link the above forms input(i mean the checkboxes value) should be taken and the link should process another .pl file in action ...

I think you could try to change the form action dynamically when user clicked the link by using jQuery. Here is a simple example:

HTML

<form id="form1" action="http://jimmy.right-pet.cc/test.php" method="post">
  <legend>Test</legend>
  <fieldset>
    <label for="name">Name</label>
    <input type="text" name="name" id="name"/>
    <input id="submit-btn" type="submit" value="submit"/>
  </fieldset>
</form>
<a id="link" href="#">change form action link</a>

JS

<script>
  $(function(){
    $("#link").click(function(){
      $("#form1").attr("action","http://jimmy.right-pet.cc/test2.php");
    });
  });
</script>

And try to use the browser dev tool to check if the form action is changed after you clicked the link.

Here is a jsFiddle demo .

Hope it helpful.

Not strictly an answer to the question, but still a solution to the problem.

From the comments:

Don't do that. Submit to one server side URI. Do it with submit buttons. Look at the value of the submit button in the form data to determine which branch of code to run on the server. – Quentin 1 hour ago

@Quentin Please Give me an example . I am new to HTML

In the HTML.

<form name="input" action="abc.pl" method="get" id="sel">
    <label>
        <input type="checkbox" name="vehicle" value="Bike">
        I have a bike
    </label>

    <label>
        <input type="checkbox" name="vehicle" value="Car">
        I have a car 
    </label>

    <input type="submit" name="sub" value="Some Action">
    <input type="submit" name="sub" value="Some Other Action">
</form>

And then in abc.pl :

use strict;
use warnings;
use CGI;

my $c = CGI->new;
my $sub = $c->param('sub');

unless ($c) {
    # No submit value was detected so either perform a default
    # action or return an error
    exit;
}

if ($c eq "Some Action") {
    # Do one thing
} elseif  ($c eq "Some Other Action") {
    # Do another thing
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM