Created
May 31, 2014 03:31
-
-
Save SoldierCorp/5c43ac92ebe0d3e793e4 to your computer and use it in GitHub Desktop.
Uso básico de jQuery ajax con PHP /// How works jQuery ajax with PHP (basic usage)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Ejemplo PHP con jQuery Ajax</title> | |
<!-- Bootstrap--> | |
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> | |
<link href="//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet"> | |
<!-- jQuery--> | |
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> | |
<script src="main.js"></script> | |
<style> | |
body { | |
margin: 80px auto; | |
width: 500px; | |
} | |
h1 { | |
text-align: center; | |
} | |
.fa { | |
display: none; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>jQuery<br/>Envio de datos por Ajax</h1> | |
<form> | |
<p> | |
<label for="username">Username</label> | |
<input class="form-control" id="username" name="username" type="text"/> | |
</p> | |
<p> | |
<label for="password">Password</label> | |
<input class="form-control" id="password" name="password" type="password"/> | |
</p> | |
<input class="btn btn-primary" type="submit" value="Login"> | |
<i class="fa fa-refresh fa-spin"></i> | |
</form> | |
<span></span> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
$('form').submit(function(e) { | |
e.preventDefault(); | |
var data = $(this).serializeArray(); | |
data.push({name: 'tag', value: 'login'}); | |
$.ajax({ | |
url: 'process.php', | |
type: 'post', | |
dataType: 'json', | |
data: data, | |
beforeSend: function() { | |
$('.fa').css('display','inline'); | |
} | |
}) | |
.done(function() { //true | |
$('span').html("Correcto"); | |
}) | |
.fail(function() { //false | |
$('span').html("Falso"); | |
}) | |
.always(function() { | |
setTimeout(function() { | |
$('.fa').hide(); | |
}, 1000); | |
}); | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$tag = $_POST['tag']; | |
if (isset($tag) && $tag !== '') { | |
if ($tag == 'login') { | |
if ($_POST['username'] === 'soldier') | |
echo true; | |
echo false; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank's.... me fue de mucha ayuda!