Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get action of submitted form

I have the following code which is triggered every time a user submits a form on my site.

I want to modify it slightly, so that it checks the action of the submission, and based on the presence of a particular keyword, runs some code.

My code is as follows:

$("form").submit(function() {
    //do some generic stuff
    var formAction = ""; //get the action of the submitted form

    if (formAction.indexOf('keyword') !== -1) {
        //do some specific stuff for these forms
    }
});         

How do I get the action of the form which triggered this call?

like image 566
Elie Avatar asked Apr 18 '13 15:04

Elie


People also ask

How do I get a form action URL?

prop() returns the full action url: To use the action attribute of a form use: $( '#myForm' ). attr( 'action' ); like @JAAulde said.

What is the action of a form?

The action attribute defines the action to be performed when the form is submitted. Usually, the form data is sent to a file on the server when the user clicks on the submit button.

What happens when a form is submitted?

First, the form and what happens when it is submitted. When you click the submit button, your form data will be sent to the file specified on the form tag - in your case, "/action_page.php". What arrives there are a number of variables (aka "key-value pairs" - but they are variables with a name and a value).

Where to send the form-data when a form is submitted?

On submit, send the form-data to a file named "action_page.php" (to process the input): The action attribute specifies where to send the form-data when a form is submitted.

What is form action in JSP?

Form Action in HTML Whenever we enter the data in the view page (user) using a browser, it will move to the backend. The HTML action needs some attributes to specify the request if; suppose we have a JSP page with servlet; the user enters the data in frontend those datas which handles using the form known as form data.

How to capture server response for form submit?

If you want to capture the server response, use AJAX or post it to an Iframe and grab what appears there after the iframe's onload () event. You can event.preventDefault () in the click handler for your submit button to ensure that the HTML form default submit event doesn't fire (which is what leads to the page refreshing).


2 Answers

$("form").submit(function() {
    //some stuff...

    //get form action:
    var formAction = $(this).attr("action");

    //some other stuff...
});   
like image 53
Adriano Carneiro Avatar answered Sep 28 '22 11:09

Adriano Carneiro


if you need Javascript without jQuery:

var action=document.getElementById('formId').action

with jQuery:

var action=$('#formId').attr('action');
like image 40
Luca C. Avatar answered Sep 28 '22 13:09

Luca C.