I'm wondering how can I submit a form via Ajax (using prototype framework) and display the server response in a "result" div. The html looks like this :
<form id="myForm" action="/getResults">
[...]
<input type="submit" value="submit" />
</form>
<div id="result"></div>
I tried to attach a javascript function (which uses Ajax.Updater) to "onsubmit" (on the form) and "onclick" (on the input) but the form is still "non-Ajax" submitted after the function ends (so the whole page is replaced by the results).
You need to stop the default action of the
onsubmit
event.For example:
More info on stopping default event behavior in Prototype »
Check out Prototype API's pages on
Form.Request
andEvent
handling.Basically, if you have this:
Your js would be, more or less:
You first need to serialize your form, then call an Ajax Updater, using POST options and pass it the serialized form data. The result will then appear in the element you sepcified.
You need to return the value false from the ajax function, which blocks the standard form submit.
}
Using onsubmit on the form also captures users who submit with the enter key.