You can submit the form without refreshing the page like this:
form.php:
<form action='profile.php' method='post' class='ajaxform'> <input type='text' name='txt' value='Test Text'> <input type='submit' value='submit'> </form> <div id='result'>Result comes here..</div>
profile.php:
<?php // All form data is in $_POST // Now perform actions on form data here and // create an result array something like this $arr = array( 'result' => 'This is my result' ); echo json_encode( $arr ); ?>
JQuery
jQuery(document).ready(function(){ jQuery('.ajaxform').submit( function() { $.ajax({ url : $(this).attr('action'), type : $(this).attr('method'), dataType: 'json', data : $(this).serialize(), success : function( data ) {
And if you want to invoke an ajax request without refreshing the page after a certain time, you can try something like this:
var timer, delay = 300000; timer = setInterval(function(){ $.ajax({ type : 'POST', url : 'profile.php', dataType: 'json', data : $('.ajaxform').serialize(), success : function(data){ for(var id in data) { jQuery('#' + id).html( data[id] ); } } }); }, delay);
And you can stop the timer at any time as follows:
clearInterval( timer );
Hope this will give you direction to complete your task.
NAVEED
source share