I’ve been into jQuery lately, it’s my first time using it, but I’ve made MAJOR improvements on my website with it. (fadeIn pages, switches, etc.)
So basically, I want to make a login form with it, but I’ve no idea how to use $.post even after having research on it.
<form action="" method="post">
<input type="text" id="username">
<input type="password" id="password">
<input type="submit" id="log" value="Log in">
</form>
Yea, that’s where I am right now, and I’d like a fancy fade animation for logging. [or atleast no page load]
EDIT: looks like so right now, it’s outputing ‘failed.’ even if I enter the right combo
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function (){
$("#loginform").submit(function(event) {
event.preventDefault();
$.post('login.php', function(data) {
$('body').hide();
$('body').html(data).show('fast');
});
});
});
</script>
</head>
<body>
<div id="all">
<form action="" method="post" id="loginform">
<input type="text" id="username"><br>
<input type="password" id="password"><br>
<input type="submit" id="log" value="Log in">
</form>
</div>
login.php:
<?php
$un = $_POST['username'];
$pw = $_POST['password'];
if($un == "huwil" and $pw == "test") {
echo "logged.";
} else {
echo "failed.";
}
?>
Loading a page after you login without having to refresh is actually possible, but I’m not sure if there’s implication with cookies etc
$.postcan be called in several ways, in your case, you need:so:
$('#formId').serialize()takes all data from theformwith id#formId, packs it in a string, and sends to the server, so in your php you can access it as$_POST['input_name'], thecallback_functionruns whenever the AJAX request is completed.