javascript - single page login system using ajax and pdo -
i want make login system without page refreshing using ajax , pdo. because im planning make single page website people can login , go profile using show , hide javascript only.
my problem when press login button nothing happens.
index.php
<?php include_once('myincludes.php'); ?> <html lang="en"> <head> <script src="bootstrap/js/jquery-1.11.0.min.js"></script> <script type="text/javascript"> $(document).ready(function () {    $('#login').click(function (e) {        e.preventdefault();         var emptyfieldsuser = $('#user').filter(function () { return this.value === '' });        var emptyfieldspass = $('#pass').filter(function () { return this.value === '' });        if (emptyfieldsuser.length) && (emptyfieldspass.length) {            alert("try again");        } else {            var data = {};            data.user_text = $('#user').val();            data.pass_text = $('#pass').val();             $.ajax({                type: "post",                url: "user.php",                data: data,                cache: false,                success: function (response) {                    alert("login sccessfully!");                }             });         }     }); }); </script> </head> <body>     <form method="post">             username: <input type="text" name="user" id="user" /><br />         password: <input type="text" name="pass" id="pass" /><br />         <button type="submit" name="login" id="login">login</button>     </form> </body> </html> myincludes.php
<?php  session_start(); include_once('connection.php'); include_once('user.php'); if(isset($_post['submit'])){     $user = $_post['user'];     $pass = $_post['pass'];     $object = new user();     $object->login($user, $pass); } ?> user.php
<?php include_once('connection.php'); class user{      private $db;      public function __construct(){         $this->db = new connection();         $this->db = $this->db->dbconnect();     }     public function login($user, $pass){         if(!empty($user) && !empty($pass)){             $st = $this->db->prepare("select * users username=? , password=?");             $st->bindparam(1, $user);             $st->bindparam(2, $pass);             $st->execute();              if($st->rowcount() == 1){                 echo "user verifies, access granted";             } else {                 echo "incorrect username or password";             }         }else{             echo "please enter username , password";         }     } } ?> connection.php
<?php class connection{     public function dbconnect(){         return new pdo('mysql:host=localhost; dbname=test', 'root', '');     } } ?> 
you have syntax error in javascript:
if (emptyfieldsuser.length) && (emptyfieldspass.length) {                           ^ if closed here and want or instead of and, negating conditions:
if (!emptyfieldsuser.length || !emptyfieldspass.length) { in php have problems:
- you not sending submit button check post should check 1 of 2 values sending or use if ($_server['request_method'] === 'post');
- your post variables $_post['user_text'];,$_post['pass_text'];setting keys in ajax function.
also note when reach javascript success function, not mean login successful. means php script ran successfully. should use response back.
Comments
Post a Comment