Search code examples
androidtitaniumtitanium-mobiletitanium-alloy

Titanium Alloy Android Login


I am having an issue with a Titanium App, the issue is only with Android, iOS is fine.

So I have a login form that queries the database, pretty straightforward. But for some reason it is not passing the params to the login script.

My JS function in app, have set the alert on the else statement to print out the $.email.value and password values which read fine. But with the current $response which is just a json it shows the values as being blank? The php form is also below. As I have said this is working fine on iOS

function login(e) {
//function to use HTTP to connect to a web server and transfer the data.
var sendit = Ti.Network.createHTTPClient({
    onerror : function(e) {
        Ti.API.debug(e.error);
        alert('There was an error during the connection');
    },
    timeout : 100000,
});
//Here you have to change it for your local ip
sendit.open('GET', 'http://soyo.taylorhoganit.co.uk/post_auth.php');
var params = {
    email : $.email.value,
    password : $.password.value,
};
sendit.send(params);
//Function to be called upon a successful response
sendit.onload = function() {
    var json = this.responseText;  
    var response = JSON.parse(json);  
    if (response.logged == true)  
    {  
        var landing = Alloy.createController("landing").getView();
        $.index.close();
        landing.open();
        // Ti.App.Properties.setString('email', $.email.value);
        // Ti.App.Properties.setString('password', $.password.value);
        //alert("Username: " + response.username);
    }  
    else  
    {  
        alert($response);  
    }  
};
};  

PHP Script

<?php  
// Connect to the database(host, username, password)  
$con = mysql_connect('replaced values');  
if (!$con)  
{  
echo "Failed to make connection.";  
exit;  
}  

$db = mysql_select_db('soyo');  
if (!$db)  
{  
echo "Failed to select db.";  
exit;  
}  

$email = $_POST['email'];  
$password = $_POST['password'];  

$sql = "SELECT * FROM users WHERE email = '" . $email . "' AND password = '" . $password .  "'";  
$query = mysql_query($sql);  

// If we find a match, create an array of data, json_encode it and echo it out  
if (mysql_num_rows($query) > 0)  
{  
$row = mysql_fetch_array($query);  
$response = array(  
    'logged' => true,    
    'email' => $row['email']  
);  
echo json_encode($response);  
}  
else  
{  
// Else the username and/or password was invalid! Create an array, json_encode it and echo it out  
$response = array(  
    'logged' => false,  
    'message' => $email + $password  
);  
echo json_encode($response);  
}  
?>  

Solution

  • You did a mistake by using $response in the alert. It is a PHP variable not JS.

     var response = JSON.parse(json);
     // ... 
     alert($response); // Here should be just `response`
    

    Edit: The another problem is that you are sending GET request while in PHP you accept POST ,so you can't get any params...