Indeed,
this is a problem that can have many different solutions depending on the specific situation
. However, one basic example could look like this:
<form class="ajax" action="/newsletter/subscribe" method=POST>
<input type="text" name="email"/>
<input type="submit"/>
</form>
<script>
$(document).on("submit", "form.ajax", function(e){
e.preventDefault();
var form = $(this);
$.ajax({
url: form.attr("action"),
type: form.attr("method"),
data: form.serialize(),
success: function(){
// handle success
},
error: function(){
// handle error
}
});
</script>
There are numerous other ways to approach this issue, but I aimed to provide a straightforward example. Thank you.