ajax request in jquery code example
Example 1: ajax request
$.ajax({
url : 'more_com.php', //PHP file to execute
type : 'GET', //method used POST or GET
data : {variable1 : "some data"}, // Parameters passed to the PHP file
success : function(result){ // Has to be there !
},
error : function(result, statut, error){ // Handle errors
}
});
// NOTE : Parameters will be available either through $_GET or $_POST according
// to the method you choosed to use.
// Here you will get your variable "variable1" this way : $_GET['variable1']
Example 2: ajax jquery example
$.ajax({
url : "file.php",
method : "POST",
data: {
//key : value
action : action ,
key_1 : value_key_1,
key_2 : value_key_2
}
})
.fail(function() { return false; })
// Appel OK
.done(function(data) {
console.log(data);
});
Example 3: $.ajax({
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.post demo</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<form action="/" id="searchForm">
<input type="text" name="s" placeholder="Search...">
<input type="submit" value="Search">
</form>
<div id="result"></div>
<script>
$( "#searchForm" ).submit(function( event ) {
event.preventDefault();
var $form = $( this ),
term = $form.find( "input[name='s']" ).val(),
url = $form.attr( "action" );
var posting = $.post( url, { s: term } );
posting.done(function( data ) {
var content = $( data ).find( "#content" );
$( "#result" ).empty().append( content );
});
});
</script>
</body>
</html>