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?
prop() returns the full action url: To use the action attribute of a form use: $( '#myForm' ). attr( 'action' ); like @JAAulde said.
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.
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).
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.
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.
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).
$("form").submit(function() {
//some stuff...
//get form action:
var formAction = $(this).attr("action");
//some other stuff...
});
if you need Javascript without jQuery:
var action=document.getElementById('formId').action
with jQuery:
var action=$('#formId').attr('action');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With