I recently started using jQuery/AJAX for submitting data to my PHP API.
Currently what I’m using is an input type=”button” in the html and then a .click() jQuery function to submit it.
I’m not quite sure it’s the best way to do it though, should I use a input type=”submit” with preventDefault() instead ?
It’s better to use
.submit()on the form instead of.click()on the submit button. In both casespreventDefaultis necessary though. The reason for that is that the user could submit the form by pressing Enter while editing some field and if you subscribed to the .click event of a button, this event won’t fire.