There was a registration form. I wanted to make an ajax request sent when submitting and the returned data was inserted into the div (Error or success). But with the form it was not possible to turn, because The action went on itself and had to remove the form tags. It seems to be all the rules, but the entered data is not possible to save (in the browser), and the actual delivery to the enterter does not work. So, how to make it so that the mold was, and ajax worked fine?
- oneUnderstood. You just had to insert javascript: void (0) into the action; . I didn’t think of it right away because I don’t know js, but here it’s just urgency. JS will learn, I promise :) - MrGaliev
- @MrGaliev, action is generally better to leave what it was, so with some errors in JS or disabled JS, at least the registration form will not break. For a similar task there are events and you need to do their handlers - vanchester
|
2 answers
Replace the form tags in place, use the onSubmit event handler, in which the ajax request will be executed. In its simplest form, you can write this:
<form onsubmit="return isExists();"> <div id="exists_result"></div> <input type="text" name="account" /> <input type="submit" value="Register"/> </form> <script> function isExists() { // выполянете ajax-запрос, получаете данные, проверяете их, выполняете требуемые действия return false; } </script>
If isExists () returns false, the form will not be submitted.
|
There is an excellent library to automate this process. Turns any regular form into ajax-form without changes in html-markup.
|