I have a php mvc website that contain controller - model - view
How I can call a function in flutter. fo example the login form of the website has:
controller function:
function checkuser()
{
$form= $_POST;
$error=$this->model->checkUser($form);
Model::sessionInit();
$userId=Model::sessionGet('userId');
if ($userId==false )
{
// header('location:'.URL.'login');
echo '<script>console.log("invalid username or password")</script>';
}
else
{
// header('location:'.URL.'userpanel');
echo '<script>console.log("successfull... you are in.")</script>';
}
if ($error !='')
{
echo '<script>console.log("there is some error")</script>';
// $data=['error'=>$error];
// $this->view('login/index',$data);
}
}
and the model is:
function checkUser($form)
{
@$email=$form['username'];
@$password=$form['password'];
@$remember=$form['remember'];
$error='';
$sql= "select * from tbl_users WHERE email=? and password=?";
$params=[$email,$password];
$result=$this->doSelect($sql,$params,1);
if (sizeof($result)>0 and !empty($email) and !empty($password))
{
Model::sessionInit();
Model::sessionSet('userId',$result['id']);
if ($remember=='on')
{
$expiry = time() + (86400 * 7);
setcookie('userIdc',$result['id'],$expiry,'/');
}
}
else {
$error='not found user.';
}
return $error;
}
the link of the controller will be /login/checkuser
I try to send request from flutter with http package but can not give a good result.
please help me about the structure of the code in flutter and how to send the data username and password to server and give feedback from there.
thanks so much. best regards
try something like below
import 'package:http/http.dart' as http;
import 'dart:convert';
userLogin() async {
Map<String, String?> userData = {
'username': 'username',
'password': 'password'
};
http.Response response = await http.post(
Uri.parse('yourUrl'),
headers: {'youHeader'},
body: json.encode(userData),
);
String jsonStr = (response.body);
try {
final mapData = jsonDecode(jsonStr);
if (mapData is Map) {
// var loginSuccess = await(AfterLogin.updatesAfterLogin(mapData)); //do other stuffs
} else {
return false;
}
} catch (e, trace) {
debugPrint("error $e, ${trace.toString()}");
return false;
}
}