# Ionic and PHP login function isn't running

**URL:** <https://forum.ionicframework.com/t/ionic-and-php-login-function-isnt-running/135074>\
**Category:** ionic-v3\
**Created:** [July 12, 2018, 6:20am UTC](https://forum.ionicframework.com/t/ionic-and-php-login-function-isnt-running/135074 "2018-07-12T06:20:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alim1990](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@alim1990](https://forum.ionicframework.com/u/alim1990)\
**Post date:** [July 12, 2018, 6:20am UTC](https://forum.ionicframework.com/t/ionic-and-php-login-function-isnt-running/135074/1 "2018-07-12T06:20:31Z")

</div>

I am making the login components of an ionic application.

Everything is working properly. But the server side part isn’t.

Here is my `authApi.ts` service:

```
loginFunction(credentials){
   console.log("Trying to login");
   //console.log(credentials)
   return new Promise(function(resolve, reject){
      let headers = new HttpHeaders();
      this.http.post(apiUrl, JSON.stringify(credentials), {headers: headers})
       .subscribe(
          res=>{
            resolve(res.json)
          }
        ),
        (err)=>{
          reject(err);
    }
});
}

```

And on login button click:

```
onLoginClick(){
let user = this.login.get('username').value;
let pass = this.login.get('password').value;
this.userData = {"username": user, "password": pass};
console.log(this.userData)
this.authService.loginFunction(this.userData).then((result)=>{
  this.signinResponse = result;
  console.log(this.signinResponse);
},
  (err)=>{
    //connection failed
  }

```

);

The server side script is:

```
<?php
error_reporting(E_ALL);
header("Access-Control-Allow-Origin: *");
class api {
    private $username ="root";
    private $password ="root";
    private $db="cdv";
    private $host = "localhost";

    public $conn;

    //Connection
    
    public function connection(){
        try{
            $this->conn = new PDO("mysql:host=$this->host;dbname=$this->db", $this->username, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->conn->exec("SET CHARACTER SET utf8");

            return $this->conn;
        }
        catch(PDOException $e){
            return $e->getMessage();
        }
        
    }

    //Login

    public function login($conn){
        $login = "SELECT username FROM login WHERE username=:user LIMIT 1";
        $execLogin = $this->conn->prepare($login);
        $execLogin->bindValue(":user", "brital");
        $execLogin->execute();
        $res = $execLogin->rowCount();

        if($res>0)
        {
            echo 1;
        }
        else{
            echo 0;
        }
    }

}

?>

```

And here is my login script:

```
<?php
require_once('../api.php');

//Getting username and password from Ionic
$username = $_POST['userData']

$newApi = new api();
$conn = $newApi->connection();
$newApi->login($conn);
?>

```

But nothing happens. Even there isn’t any error at the console. And no files are shown in the network tab.

Currently, I am testing the functions that’s why I am returning 1 and 0 as response.

The button html code is:

`<button ion-button full (click)="onLoginClick()" [disabled]="!login.valid" >LOGIN</button>`

---

<div class="post-metadata">

**Author:** ![avishai\_peretz\_dev](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@avishai\_peretz\_dev](https://forum.ionicframework.com/u/avishai_peretz_dev)\
**Post date:** [July 12, 2018, 9:01am UTC](https://forum.ionicframework.com/t/ionic-and-php-login-function-isnt-running/135074/2 "2018-07-12T09:01:12Z")

</div>

Add your html button code

---

<div class="post-metadata">

**Author:** ![alim1990](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@alim1990](https://forum.ionicframework.com/u/alim1990)\
**Post date:** [July 12, 2018, 10:27am UTC](https://forum.ionicframework.com/t/ionic-and-php-login-function-isnt-running/135074/3 "2018-07-12T10:27:23Z")

</div>

Added. You can recheck the post.

---

<div class="post-metadata">

**Author:** ![avishai\_peretz\_dev](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@avishai\_peretz\_dev](https://forum.ionicframework.com/u/avishai_peretz_dev)\
**Post date:** [July 12, 2018, 10:47am UTC](https://forum.ionicframework.com/t/ionic-and-php-login-function-isnt-running/135074/4 "2018-07-12T10:47:15Z")

</div>

where is the php script that call to function login($conn)?

---

<div class="post-metadata">

**Author:** ![alim1990](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@alim1990](https://forum.ionicframework.com/u/alim1990)\
**Post date:** [July 12, 2018, 10:51am UTC](https://forum.ionicframework.com/t/ionic-and-php-login-function-isnt-running/135074/5 "2018-07-12T10:51:43Z")

</div>

It exists in the post. You can check them as well.

---

<div class="post-metadata">

**Author:** ![avishai\_peretz\_dev](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@avishai\_peretz\_dev](https://forum.ionicframework.com/u/avishai_peretz_dev)\
**Post date:** [July 12, 2018, 10:54am UTC](https://forum.ionicframework.com/t/ionic-and-php-login-function-isnt-running/135074/6 "2018-07-12T10:54:20Z")

</div>

> [@alim1990](#):
>
> \<?php require\_once(‘…/api.php’); //Getting username and password from Ionic $username = $\_POST[‘userData’] $newApi = new api(); $conn = $newApi-\>connection(); $newApi-\>login($conn); ?\>

```auto
<?php
require_once('../api.php');

//Getting username and password from Ionic
//$username = $_POST['userData'];// **NOT OK!!!**

$name = $_POST['username'];
$password = $_POST['password'];

$newApi = new api();
$conn = $newApi->connection();
$newApi->login($conn);
?>

```

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:30pm UTC](https://forum.ionicframework.com/t/ionic-and-php-login-function-isnt-running/135074/7 "2021-07-19T21:30:00Z")

</div>

Closed on July 19, 2021, 9:30pm UTC


