I can recommend that you take a look at the AJAX documentation ( link ), since as you can see in your code, you execute the ajax function and then change the location of the page. Because AJAX by default is asynchronous , which means that this line $(location).attr('href','checkout2.php');
will run without waiting for the AJAX call to end.
I can also recommend that when you make an AJAX call, you block the button that produces this call so that the user does not press it several times.
I'll show you an example:
$(document).ready(function() {
$('#send').click(function() {
var name = $('#name').val();
$.ajax({
method: "GET",
url: "http://jsfiddle.net/echo/jsonp/",
dataType: 'JSONP',
data: { name: name}
}).done(function( result ) {
// Si la respuesta es correcta, entonces cambias el location
//$(location).attr('href','checkout2.php');
alert('Llamado finalizado');
}).fail(function(msg) {
// Sia la respuesta es incorrecta, emites un error al usuario
alert('Error!');
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="name" />
<button type="button" id="send">Send</button>