logar no banco com ajax e jquery
Olá pessoal, fiz 2 formularios de login em jquery e ajax, um eu vi na internert o modelo então fiz umas adaptações e funcionou com meu banco de dados. porem o outro que fiz baseado nele não funcionou. vou postar aqui pra voces o funcionando e o não funcionando, alguém pode me dar uma ajuda?, dizer que erro é esse? ele simplesmente não loga e não aparece erros no console.
<!doctype html>
<html>
<?php
require_once"config.php";
?>
<head>
<title>APRENDIZ DE JS</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#but_submit").click(function(){
var username = $("#meu_nome").val().trim();
var password = $("#minha_senha").val().trim();
if( username != "" && password != "" ){
$.ajax({
url:'pesquisar.php',
type:'post',
data:{username:username,password:password},
success:function(response){
var msg = "";
if(response == 1){
window.location = "home.php";
}else{
msg = "Invalid username and password!";
}
$("#message").html(msg);
}
});
}
});
});
</script>
</head>
<body>
<div class="container">
<div id="div_login">
<h1>Login</h1>
<div id="message"></div>
<div>
<input type="text" class="textbox" id="meu_nome" name="meu_nome" placeholder="Username" />
</div>
<div>
<input type="password" class="textbox" id="minha_senha" name="minha_senha" placeholder="Password"/>
</div>
<div>
<input type="button" value="logar" name="but_submit" id="but_submit" />
</div>
</div>
</div>
</body>
</html>
/ Container /
.container{
width:40%;
margin:0 auto;
}
/ Login /
#div_login{
border: 1px solid gray;
border-radius: 3px;
width: 470px;
height: 270px;
box-shadow: 0px 2px 2px 0px gray;
margin: 0 auto;
}
#div_login h1{
margin-top: 0px;
font-weight: normal;
padding: 10px;
background-color: cornflowerblue;
color: white;
font-family: sans-serif;
}
#div_login div{
clear: both;
margin-top: 10px;
padding: 5px;
}
#div_login .textbox{
width: 96%;
padding: 7px;
}
#div_login input[type=submit]{
padding: 7px;
width: 100px;
background-color: lightseagreen;
border: 0px;
color: white;
}
#message{
width:100%;
text-align:center;
color:red;
}/ media /
@media screen and (max-width:720px){
.container{
width: 100%;
}
#div_login{
width: 99%;
}
}
<?php
require_once "config.php";
// AQUI É O PESQUISAR PHP
$uname = mysqli_real_escape_string($con,$_POST['username']);
$password = mysqli_real_escape_string($con,$_POST['password']);
if ($uname != "" && $password != ""){
$sql_query = "SELECT count(*) as cntUser FROM usuarios WHERE username='".$uname."' and password='".$password."'";
$result = mysqli_query($con,$sql_query);
$row = mysqli_fetch_array($result);
$count = $row['cntUser'];
if($count > 0){
$_SESSION['uname'] = $uname;
echo 1;
}else{
echo 0;
}
}
<?php
require_once "config.php";
// AQUI É A HOME.PHP// Check user login or not
if(!isset($_SESSION['uname'])){
header('Location: index.php');
}
// logout
if(isset($_POST['but_logout'])){
session_destroy();
header('Location: index.php');
}
?><!doctype html>
<html>
<head>
<title>SEJA BEM VINDO!</title>
</head>
<body>
<h1>PÁGINA INICIAL</h1>
<form method='post' action="">
<input type="submit" value="SAIR" name="but_logout">
</form>
</body>
</html>
<?php// ESSE AQUI É A CONFIG.PHP
session_start();
$host = "localhost"; / Host name /
$user = "root"; / User /
$password = ""; / Password /
$dbname = "login2"; / Database name /
$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
Então pessoal, até aqui funciona normal .
agora a linha abaixo é oque não funciona e eu vou postar só a index e o css porque repete o de cima.
se alguém poder me ajudar eu fico grato. desde já muito obrigado
<html>
<?php
require_once"config.php";
?>
<head>
<title>Formulario de login </title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script src="jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#but_submit").click(function(){
var username = $("#meu_nome").val().trim();
var password = $("#minha_senha").val().trim();
if( username != "" && password != "" ){
$.ajax({
url:'pesquisar.php',
type:'post',
data:{username:username,password:password},
success:function(response){
var msg = "";
if(response == 1){
window.location = "home.php";
}else{
msg = "Invalid username and password!";
}
$("#message").html(msg);
}
});
}
});
});
</script>
</head>
<body>
<form id="formulario" method="post">
<fieldset>
<h2>Configuracoes da conta</h2>
<h3>Area de login </h3>
<input type="text" id="meu_nome"name="meu_nome" placeholder="username" />
<input type="password" id="minha_senha " name="minha_senha" placeholder="digite sua senha" />
<input type="submit" class="next acao" value="Logar" />
</fieldset>
</body>
</html>
agora é o css
*{margin:0; padding:0;}
html{
height:100%;
background: linear-gradient(rgba(38, 128, 101, 0.9), rgba(52, 177, 140, 0.9));
}.erros, .ok
{
width:390px;
padding:5px;
background:rgba(238, 249, 113, 0.9);
margin: 20px auto 0 auto;
border: 1px solid rgba(222,239,10,0.9);
border-radius: 4px;
color: #333;
}.ok{background: rgba(155,222,143,0.9);border-color: rgba(68, 171, 50, 0.9);color:white;}
body{
font-family: "Trebuchet MS", tahoma, arial;
}
#formulario{
width:500px;
margin:40px auto;
text-align:center;
position:relative;
}#formulario fieldset{
background:white;
border:0 none;
border-radius:5px;
-moz-border-radius:5px;
-webkit-border-radius:5px;
box-shadow:0 0 15px 1px rgba(0,0,0, 0.4);
padding:20px 30px;
box-sizing: border-box;
width:80%;
margin:0 10%;
position:absolute;
}#formulario input, #formulario textarea{
padding:10px;
border:1px solid rgba(38, 128, 101, 0.9);
border-radius:4px;
-moz-border-radius:4px;
-webkit-border-radius:4px;
outline:none;
box-sizing: border-box;
width:100%;
font:14px "Trebuchet MS", tahoma, arial;
color:#090;
margin-bottom:10px;
}#formulario .acao{
width:100px;
background:rgba(38, 128, 101, 0.9);
color:white;
text-transform:uppercase;
font-weight:bold;
padding:12px 0;
float:left;
margin-right:5%;
}#formulario .acao:hover{background:rgba(52, 177, 140, 0.9); cursor:pointer;}
Discussão (11)
Carregando comentários...