New to AJAX, I've built a form that contains AJAX that will send the data to another page. I'm not sure if I have to include a plugin for multiple ajax-requests or if this is done automatically. The problem so far is $("#userForm")[0].reset(); will wait until it has completely successfully before clearing the input field. I'm hoping to get it to send and clear immediately, allowing the user to enter request rapidly and building up a queue for the request to be completed.
Below is my form/ajax
<html>
<head>
<title>fastPage</title>
</head>
<body>
<form id='userForm'>
<div><input type='text' name='name' /></div>
<div><input type='submit' value='Submit' /></div>
</form>
<div id='response'></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "></script>
<script>
<script>
$(document).ready(function(){
$('#userForm').submit(function(){
// show that something is loading
//$('#response').html("<b>Loading response...</b>");
// Call ajax for pass data to other place
$.ajax({
type: 'POST',
url: 'slowPage.php',
data: $(this).serialize() // getting filed value in serialize form
})
.done(function(data){ // if getting done then call.
// show the response
//$('#response').html(data);
$("#userForm")[0].reset();
})
.fail(function() { // if fail then getting message
// just in case posting your form failed
alert( "Posting failed." );
});
// to prevent refreshing the whole page page
return false;
});
});
</script>
</body>
</html>
the slowPage.php is just to test the ajax calls out.
<!DOCTYPE html>
<html>
<head>
<title>slowPage</title>
</head>
<?php
session_start();
echo "
<form action='slowPage.php' method='POST'>
<input type='text' name='name'>
<button type='submit'>Search</button>
</form>";
sleep(10);
if($_POST){
echo $_POST['name'];
}
?>
Can anyone point me in the right direction to accomplish this?