Reputation:
$(document).ready(function() {
var login = $("#login").val();
var password = $('#password').val();
$('.login-button').click(function() {
alert(login);
});
});
HTML
<form method="post" class="login"> <p id="login-error"></p>
<p>
<label for="login">Username:</label>
<input type="text" name="login" id="login" placeholder="username">
</p>
<p>
<label for="password">Password:</label>
<input type="password" name="password" id="password" placeholder="password">
</p>
<p class="login-submit">
<button class="login-button">Login</button>
</p>
<p class="forgot-password">Fill your username and password.</p> </form>
It shows nothing! What's up with it? Any solution? The id names are correct
EDIT: I have another problem. Dont want to open another question for that. I'm trying to get ajax answer from validate.php (it is in /views/admin/validate.php - it runs when opening domain.com/validate)
$(document).ready(function() {
$('.login-button').click(function() {
var login = $("#login").val();
var password = $('#password').val();
$.ajax({
type: 'POST',
url: '/view/admin/validate.php',
data: {
login : login,
password : password
},
success: function(data){
$('#login-error').html(data);
}
});
});
});
validate.php
<?php
session_start();
$user = mysql_real_escape_string($_POST['login']);
$password = mysql_real_escape_string(sha1($_POST['password']));
$query = mysql_query("SELECT * FROM `users` WHERE name = '$user' AND pass = '$password' AND privileges = 'superuser'");
$num_rows = mysql_num_rows($query);
if($num_rows == '0') {
echo "Username and Password are incorrect! (Maybe you don't have permission!)";
}
elseif($num_rows == '1') {
$expire = time()*60*60*60*60;
setcookie("user","$user",$expire);
$_SESSION['user'] = $user;
include '/views/admin/admin.php';
}
?>
It should return Username and Password are incorrect! (Maybe you don't have permission!) - but it doesn't.. Any solution??
Upvotes: 0
Views: 165
Reputation: 116
Try it :
$('.login-button').click(function() {
alert($("#login").val());
});
or :
$('.login-button').click(function() {
var login = $("#login").val();
alert(login);
});
or :
var login = "";
$('.login-button').click(function() {
login = $("#login").val();
alert(login);
});
Upvotes: 0
Reputation: 10452
$('.login-button').click(function() {
var login = $('#login').val();
alert(login);
});
Upvotes: 0
Reputation: 888107
var login = $("#login").val();
When this line runs, the input is empty.
You need to get the value after the user types something.
Upvotes: 5