How can i submit a form using button class?

23 Nov

How can i submit a form using button class?

Assume that the form is as shown below.
Id of the form is block_form, and also we added a class named
block to the button.

<form id="block_form" method="post">		
	<input type="hidden" name="server_ip" value="y.y.y.y">
	<button class="btn btn-danger block" type="submit">Block</button>
</form>

Using Jquery we can submit the form by binding the class name to the click event. You can see the code for the form submition below.

$( ".block" ).click(function(event) {
	event.preventDefault();
	$.post( "ajax.php", 
		'action=block&'+ $('#block_form').serialize(),
		function( data ) {
			alert(data)
		}

	});
});

Leave a Reply

Your email address will not be published. Required fields are marked *