views:

197

answers:

5

I want an html form to do nothing after it has been submitted.

action="" 

is no good because it causes the page to reload.

Basically i want an ajax function to be called whenever a button is pressed or someone hits "enter" after typing it data. Yes i could drop the form tag and add just call the function from the button's onclick event, but i also want the "hitting enter" functionality without getting all hackish.

+2  A: 

By using return false; in the javascript that you call from the submit button, you can stop the form from submitting.

Basically, you need the following HTML:

<FORM onSubmit="myFunction(); return false;">
<INPUT TYPE="SUBMIT" VALUE="Submit">
</FORM>

Then the supporting javascript:

<SCRIPT LANGUAGE="JavaScript"><!--
function myFunction() {
    //do stuff
}
//--></SCRIPT>

If you desire, you can also have certain conditions allow the script to submit the form:

<FORM onSubmit="return myFunction();">
<INPUT TYPE="SUBMIT" VALUE="Submit">
</FORM>

Paired with:

<SCRIPT LANGUAGE="JavaScript"><!--
function myFunction() {
    //do stuff
    if (condition)
        return true;

    return false;    
}
//--></SCRIPT>
JGB146
Missing `function` keyword: `function myFunction() { }`
John Kugelman
A: 

As part of the button's onclick event return false which will stop the form from submitting.

ie:

function doFormStuff() {
    // ajax function body here
    return false;
}

And just call that function on submit button click. There are many different ways.

Rosco
+1  A: 

How about

<form id="my_form" onsubmit="the_ajax_call_function(); return false;">
 ......
</form>
ZX12R
A: 

use jquery. Maybe put a div around your elements from the form. Then use preventDefault() and then make your ajax calls

controlfreak123
A: 

u can use the following html

<FORM onSubmit="myFunction(); return false;">

Keyur Shah