I have a file.php and inside a function. Javascript Side, I use Ajax.
$.ajax({
type: "POST",
url: "file.php",
data: {param1: params1, param2: params2...},
complete: function(data){
/* code */
}
});
Now in the file file.php, a function login exists. My question how can I call the function login ? Thanks.
function ft_login($login, $password) {
$u = new KVObject();
$u->collection = "users";
$u->loadFormArray(["login" => $login, "password" => $password]);
if ($u->obj_id == null)
return ["ERROR" => "Bad creditial"];
if ($u->status == "blocked")
return ["ERROR" => "Account need to be activated"];
$token = md5(time());
$_SESSION[$token] = $u->dataObject;
return ["token" => $token, "user" => $u->dataObject];
}
The whole php file is executed. So if you call ft_login inside file.php it gets executed. You can pass your parameters to that function.
Dont forget to json_decode your parameters.
You can pass another parameter instructing the PHP code what to do
$.ajax({
type: "POST",
url: "file.php",
data: {request: 'login', password:userid, password:password},
complete: function(data){
}
});
And then in the file.php
PHP code
<?php
function ft_login($login, $password) {
. . .
}
if ( isset($_POST['request'] && $_POST['request'] == 'login' ) {
if ( isset($_POST['login'], $_POST['password']) ) {
$result = ft_login($_POST['login'], $_POST['password']);
// do whatever with $result
}
}