Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit HTML form, perform javascript function (alert then redirect)

Tags:

People also ask

How do you call a JavaScript function when a form is submitted?

To call and run a JavaScript function from an HTML form submit event, you need to assign the function that you want to run to the onsubmit event attribute. By assigning the test() function to the onsubmit attribute, the test() function will be called every time the form is submitted.

What is the purpose function of HTML form?

An HTML form is used to collect user input. The user input is most often sent to a server for processing.

How can I submit a form without redirecting?

To submit an HTML form without redirection, we can redirect to an invisible iframe. And then we set the form's target attribute to the ID of the hidden iframe to redirect to it after form submission is done.

Can a form action be a JavaScript function?

In an HTML form, the action attribute is used to indicate where the form's data is sent to when it is submitted. The value of this can be set when the form is created, but at times you might want to set it dynamically.


I have a JavaScript function that works great when I call it through an 'onClick' method. Basically what it does is pull data from a html form, and then based on that data, redirects to another page.

However, when I press the 'enter' button to submit the form/call the JavaScript function, instead of using a onClick button to call the JavaScript function, it doesn't respond as I would like it to.

I would like for when a user presses the enter button on their keyboard, the same thing happens as if they were to click the 'proceed button' on by page (which calls the function via onClick)

Here is my code:

The JS Function:

function completeAndRedirect(){     alert('You\'re nearly there! To complete your entry, you simply need to tap \'continue\' on the next page. Good luck!');     location.href='http://google.com/?SID='+'<? echo $CATEGORY; ?>'+','+'<? echo $PLATFORM; ?>'+','+'<? echo $DEVICE; ?>'+'&email='+document.forms[0].elements[0].value; } 

Here is the form:

<form action="" method="post" onsubmit="completeAndRedirect()">     <input type="text" id="Edit1" style="width:280; height:50; font-family:'Lucida Sans Unicode', 'Lucida Grande', sans-serif; font-size:22px"> </form> 

Huge thanks to anyone who can help out!