0

this is js code i am calling "is_loggedin" from ajax in php folder in login.php but it always gives undefined index action

$(document).ready(function(){
$('.carousel').carousel({
    pause: "false"
});
});

$("#loginId").click(function(){
    $("#modalLoginId").modal("show");
});

$("#registerId").click(function(){
    $("#modalRegisterId").modal("show");
});

$("#login-btn").click(function () {

    var loginpasswordJSON = JSON.stringify({
        "username": $('#login_username').val(),
        "password": $('#login_password').val(),
    });

    alert(loginpasswordJSON);

    console.log ("Login Started");

    $.ajax({
            type: 'POST',
            url:  'php/login.php',
            dataType: 'json', // data type of response
            data: {action:"success"},
            contentType:'application/json',
            success: function(data){
        alert("successfully logged In")
            }
            //error: function(jqXHR, textStatus, errorThrown){
            //  handleErrors('Error: ' , textStatus , errorThrown);
        //  }


        });
console.log ("Login Ends");
    return false;
}
);

login.php

<?php
$action = $_POST["action"];
if($action=="success") {
is_loggedin();
}
class USER
{


    private $db;

    function __construct($DB_con)
    {
      $this->db = $DB_con;
    }

    public function register($fname,$lname,$uname,$umail,$upass)
    {
       try
       {
           $new_password = password_hash($upass, PASSWORD_DEFAULT);

           $stmt = $this->db->prepare("INSERT INTO users(user_name,user_email,user_pass)
                                                       VALUES(:uname, :umail, :upass)");

           $stmt->bindparam(":uname", $uname);
           $stmt->bindparam(":umail", $umail);
           $stmt->bindparam(":upass", $new_password);
           $stmt->execute();

           return $stmt;
       }
       catch(PDOException $e)
       {
           echo $e->getMessage();
       }
    }


    public function login($uname,$umail,$upass)
    {
      echo "ma";
       try
       {
         echo "pass";
          $stmt->execute(array(':uname'=>$uname, ':umail'=>$umail));
          $stmt = $this->db->prepare("SELECT * FROM gy_user_detail WHERE user_name=:uname OR user_email=:umail LIMIT 1");
          $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
          if($stmt->rowCount() > 0)
          {
             if(password_verify($upass, $userRow['user_pass']))
                //$_SESSION['user_session'] = $userRow['user_id'];

             {
                return true;
             }
             else
             {
                return false;
             }
          }
       }
       catch(PDOException $e)
       {
           echo $e->getMessage();
       }
   }

   public function is_loggedin()
   {
     echo "hahah";
    //  if(isset($_SESSION['user_session']))
    //  {
      //   return true;
    //  }
   }

   public function redirect($url)
   {
       header("Location: $url");
   }

   public function logout()
   {
        session_destroy();
        unset($_SESSION['user_session']);
        return true;
   }
}
?>
2
Contributors
4
Replies
23
Views
1 Year
Discussion Span
Last Post by diafol
2

WHere are you sending data in your ajax routine? You're just sending "success". loginpasswordJSON is not being sent.
Also you're calling a class method is_loggedin() like a regular function, without instantiating the object first. Why is the class in the php script? This class should be in a file of its own. You just 'require' the class and then use it - but you're not even instantiating it.
Also you can't check for session data without a session_start(). If the PHP snippet shown is the entire code for the PHP, then this is seriously f'ed up.

0

Thanks for replying how would you appraoch it,i am trying to learn and do things now in Objected Oriented way.

0

things become bit complicated in OOP i have been banging my head for a day
help me out..

1

Heh heh OOP is a big headache! Place class on its own in a file. Require the class in your php file. Instantiate the class and call whatever method you need, e.g.:

$u = new User;
$u->someMethod();

I've already mentioned having session_start()

Votes + Comments
Diafol helpful as always,,, :)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.