Any help please , i create a simple form for login in ionic 2 and a php file to authenticate this login but i dont how to run the php file from my ionic app i use may i should add some script in the ts file but honsetly i dont how to do it
this are my html :
We donāt see much of you code here but here how it should normally works. PHP must run on a seperate server, maybe local eg: wamp server. Then in your php you must handle http request like POST, GET depending on your needs. Normally with a login you do a POST request. On Ionic side you must import Http from angular/http and then use it to make your call to your php script file.
Login.ts
this.data = {username: "username", password: "password"}; // this should be replace by your form data
this.http.post('url_to_php_script', JSON.stringify(this.data), {headers: {'Content-Type': 'application/json'}})
.map(
res => console.log("res", res.json()), // res.json() -> this is your response from your php script
error => console.log("err", error) // if there's any error with you request
);
PHP
$rawData = file_get_contents("php://input");
$rawData in the PHP script should contain everything about your request sent from your Ionic app. You should read more about it!
You could also take this to another level and create your own providers to make your request inside it and make calls to your provider from your page component instead of calling http.post directly.
@skoff first thx u for your reply
second : where i should insert this.data⦠inside the constructor???
and i should import the http like this : import {Http} from āangular/httpā;
it give an error : ā[ts] Cannot find module āangular/httpā.ā
@Gatewayapp in my example this.data represent your form data, the best way to do it is to in fact create an object that will encapsulate all your form data which you can then send to your php script.
login.ts
import {Http} from '@angular/http';
...................................
export class HomePage {
loginData = {};
...................................
login() {
this.http.post.('url_to_php', JSON.stringify(this.loginData))
.subscribe(res => {
// res.json() will contain the response from your php script
}
});
}
The login() method will be called from the Login button. Then in this method it gets this.loginData which contains all your form data. Because in each input of your form you set [(ngModel)]=āloginData.Usernameā etc. I think you should more about the basics of Angular 2 and Ionic 2 also it could help you understand better how it works!
Hi I am totally new to ionic,
I was wondering, would this still work with all the updates made to ionic 2, and may i have a sample of how the php file would look like.