Form action - when calling HREF

I have a basic question about the action of a form.

Consider the code below, just ignore the syntax, if any.

<form action="demo_form.asp" method="get">
 <a href="test.asp">Code testing</a>
 First name: <input type="text" name="fname" /><br />
 Last name: <input type="text" name="lname" /><br />
 <button type="submit">Submit</button><br />
</form> 

Demo_form.asp is called when the form is submitted, and I can process the variables in the form as shown below in demo_form.asp,

request.GET('fname')

How to handle variables in the form when test.asp is called via HREF..Can I use the same GET or POST method?

+3
source share
2 answers

If you call test.aspby reference (or href, as you say), you can only process variables using the GET method. Therefore, if you have, for example test.asp?fname=bob&lname=smith, these GET variables will be available in test.asp

You cannot do this with POST data.

0
source

- GET. , , .

, a, onclick JavaScript DOM, href click . GET.

HTML:

<form action="test.asp" id="theForm">
     <a href="test.asp?" id="link">Code testing</a>
     First name: <input type="text" name="fname" /><br />
     Last name: <input type="text" name="lname" /><br />
</form>

JQuery

$('#link').click(function() {
    var href = "";
    $('#theForm > input').each(function(event) {
        event.preventDefault();
        href = $('#link').attr("href") + "&" + 
            $(this).attr("name") + "=" + $(this).attr("value");
        $('#link').attr("href", href);
    });
    $(this).click();  // or window.location = $(this).attr("href");
});

, , , DOM , .

, , , .

+1

All Articles