How to redirect to a form of the app after validating the username and password in Android

0

I do not know how to explain myself but I will try to do it in the best possible way.

I have been designing Android apps with the help of Eclipse and Cordova. My app can connect to an external database (web hosting). What it does is verify if the user exists and give continuity to it. The process I do in PHP but the PHP file is outside the apk since it does not work if I work from there.

In other words, first I create the form iniciarsesion.html with the POST action at www.paginaweb.com/procesoiniciarsesion.php and it works successfully, now the problem arises when I want to return to another form within the app since As I check from the outside, I do not know how to get back to the app.

Normally what I have done is once I verify from the outside, I continue with forms externally, but I would like the forms to be maintained in the application to avoid higher costs in mobile data plans.

Thanks programmers

    
asked by Luis A. Velasco 13.04.2017 в 06:28
source

1 answer

0

You should not go to an external site to do the checkup. But you must use the external site as if it were an API.

In a PHP file that checks if the user exists, have it print OK or ERROR, in the case of correct or incorrect validation.

In the App use Jquery or similar and make a POST request as follows.

Example with Jquery:

$(docuement).ready(function(){
  function comprobar_user(email,pass){
       $.post("paginaphpexterna",{"email":email,"pass":pass}, function(data){
           if (data == "OK"){
               alert("usuario valido");
           }else{
               alert("usuario invalido");
           }
       })
  }
  comprobar_user("[email protected]","123456");
})

What can happen to you, that what I have just written does not work and it is because it is going to throw you Cross Origin error, what do you mean? That the page.php is in a certain domain and the html of the app in a different one. Then for security the post will not be executed. How is it solved? On the PHP page you must add a specific heading

header('Access-Control-Allow-Origin: *');
header("Content-type:application/json")  

and finally, instead of OK and ERROR, you have to have it return a JSON. that is, {"answer": "OK"} or {"answer": "ERROR"}

and the code modify it as follows.

$(docuement).ready(function(){
  function comprobar_user(email,pass){
       $.post("paginaphpexterna",{"email":email,"pass":pass}, function(data)
       {
           if (data.repuesta == "OK"){
               alert("usuario valido");
           }else{
               alert("usuario invalido");
           }
       })
  }
  comprobar_user("[email protected]","123456");
})
    
answered by 14.04.2017 в 03:54