Ionic2 create .php to get data for show in page


#1

I want to know how to create php to get data for show home.html

and I get `Error data is not defined

I don’t know this correct? ,please check this. I have not Idea to create .php ,I want to set $path to keep my url.

<ion-content>
    <ion-list inset>
        <ion-item>
            <ion-label>Username</ion-label>
            <ion-input [(ngModel)]="data.username" type="text"></ion-input>
        </ion-item>
    </ion-list>
    <div padding>
        <button ion-button block (click)="getRepos()">Search</button>
    </div>
    <ion-card *ngFor="let repo of foundRepos" >
        <ion-card-header>
            {{ repo.data.name }}
        </ion-card-header>
        <ion-card-content>
            {{ repo.data.description }}
        </ion-card-content>
    </ion-card>
</ion-content>

.

import { Component } from "@angular/core";
import { NavController } from 'ionic-angular';
import { Http } from '@angular/http';

@Component({
    selector: 'page-home',
    templateUrl: 'home.html'
})
export class HomePage {
  public foundRepos;

  constructor(public http: Http) {
        this.data = {};   >>>>>>>data is not defined
        data.username = ''; >>>>>data is not defined
        this.http = http;
    }

   getRepos() {
        var link = 'http://localhost/github.php';
        var data = JSON.stringify({username: this.data.username}); <<<<this.data.username ()

        this.http.get(link, data) <<<<data is not defined
        .subscribe(data => {
         this.foundRepos = data.json();
       },
        err => console.error(err),
        () => console.log('getRepos completed')
    );
  }

}

github.php

<?php
    $postdata = file_get_contents("php://input");
    if (isset($postdata)) {
        $request = json_decode($postdata);
        $username = $request->username;

        if ($username != "") {
            $path = "https://api.github.com/users/".$username."/repos";
            echo $username;
        }
        else {
            echo "Empty username parameter!";
        }
    }
    else {
        echo "Not called properly with username parameter!";
    }
?>