php form submit without refresh code example

Example 1: prevent form submit on page refresh php\

//Using Javascript
<script>
if ( window.history.replaceState ) {
        window.history.replaceState( null, null, window.location.href );
    }
</script>

Example 2: submit form without page refresh using ajax jquery and php

$(document).ready(function(){
		$('button').click(function(event){
			event.preventDefault();
			var	name = $('#name').val();
			var	mobile = $('#mobile').val();
			var	address = $('#address').val();
			var	city = $('#city').val();
			$.ajax({
			    type: "POST",
			    url: "upload.php",
			    data: { name:name, mobile:mobile, address:address, city:city },		    
			    dataType: "json",
			    success: function(result){
			        			    }
			});
		});
	});

Example 3: php do not refresh page after submit post

// add this code to javascript
<script>
    if ( window.history.replaceState ) {
        window.history.replaceState( null, null, window.location.href );
    }
</script>

Tags:

Php Example