I have the following form (content has been removed due to irrelevance):
<form id="ticketinfo" class="form-horizontal" action="<?php echo base_url('customerinfo'); ?>" method="post">
<fieldset>
<input type="submit" value="Add Ticket" class="btn-style-green" id="btnAdd" />
<input type="submit" value="Next >>" class="purchase-your-ticket col-md-3" id="next">
</fieldset>
</form>
This is my JavaScript:
$('#btnAdd').click ( function () {
$('form#ticketinfo').submit (
function (event)
{
event.preventDefault(); //Avoid that the event 'submit' continues with its normal execution, so that, we avoid to reload the whole page
$.post(
url+"ticketinfo/add", //The variable 'url' must store the base_url() of our application
$("form#ticketinfo").serialize(), //Serialize all the content of our form to URL format
function (response)
{
}
);
});
});
What it does:
When I click on #btnAdd
it submits the form via AJAX with the specified URL. When I click #next
it makes a regular (not AJAX) post to the target URL specified in the form's "action" executes a block of code and redirects me to another page.
Both buttons work the way they should that is until after I click #btnAdd
. When I click #next
after I have clicked #btnAdd
the form submits via AJAX to the URL specified in the jQuery function instead of making a normal post to the server.
In short #next
performs the job of #btnAdd
if I click #btnAdd
first.
What both buttons should really do:
`#btnAdd` - submit form via AJAX
`#next` - submit form via regular post and redirect user.
I should also add that I want the buttons validation events to be triggered when the form submits either way, instead of having to write extra code to handle form validation.