Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Update Remote MySQL

I have just started to dive into mobile application development, and I'm brand new to Titanium. I'm loving everything I'm seeing so far, but I'm confused about how I can update my server-side MySQL database.

I'm building an application that will function as a member-rewards system for customers of a restaurant. In short, the application will allow customers to check off certain items when they purchase them. In order to check off an item you need to enter a passcode that matches one in the server-side database. To avoid the duplicate use of a single passcode we can only have each passcode used once. So, the application needs to:
1)Check the passcode against the server-side database.
2)Set the passcode status to false in the server-side database so that it can't be used again.

I understand how to use JSON to grab rows from my server-side MySQL. Is there a way I can pass variables from my application to server-side in order to return some JSON based on those variables back to the application?

— asked October 13th 2010 by Benjamin Bartling
  • database
  • mobile
  • mysql
0 Comments

2 Answers

  • Accepted Answer

    I had this question the other day. Here is my login script I came up with. It is a basic login system. In your PHP file, just use $_POST['param name'] to get the data

    var loginReq = Titanium.Network.createHTTPClient();
    loginReq.onload = function()
    {
        var json = this.responseText;
        var response = JSON.parse(json);
        if (response.logged == true)
        {
            username.blur();
            password.blur(); 
            Ti.App.fireEvent('grantEntrance', {
                name:response.name,
                email:response.email
            });
        }
        else
        {
            alert(response.message);
        }
    };
    
    loginReq.onerror = function()
    {
        alert("Network error");
    };
    
    /*
    * Login Button Click Event
    */
    loginBtn.addEventListener('click',function(e)
    {
        if (username.value != '' && password.value != '')
        {
            loginReq.open("POST","http://localhost:8888/post_auth.php");
            var params = {
                username: username.value,
                password: Ti.Utils.md5HexDigest(password.value)
            };
            loginReq.send(params);
        }
        else
        {
            alert("Username/Password are required");
        }
    });
    

    and the php

    <?php
    if (isset($_POST['username']))
    {
        $con = mysql_connect('localhost','root','root');
        if (!$con)
        {
            echo "Failed to make connection.";
            exit;
        }
    
        $db = mysql_select_db('test');
        if (!$db)
        {
            echo "Failed to select db.";
            exit;
        }
    
        $username     = $_POST['username'];
        $password     = $_POST['password'];
    
        $sql         = "SELECT * FROM users WHERE username = '" . $username . "' AND password = '" . $password . "'";
        $query         = mysql_query($sql);
        if (mysql_num_rows($query) > 0)
        {
            $row = mysql_fetch_array($query);
            $response = array(
                'logged'     => true,
                'name'         => $row['name'],
                'email'     => $row['email']
            );
            echo json_encode($response);
        }
        else
        {
            $response = array(
                'logged'     => false,
                'message'     => 'Invalid Username and/or Password'
            );
            echo json_encode($response);
        }
    }
    ?>
    
    — answered October 13th 2010 by Ronnie Swietek
    permalink
    1 Comment
    • Thanks for the answer Ronnie. That is exactly what I was looking for and you just cleared up a lot of things for me. :)

      — commented October 13th 2010 by Benjamin Bartling
  • hello i'm trying with the same code but the result is
    "network error" identifie in the alert loginReq.onerror = function()
    {
    alert("Network error");
    };

    help me right now plz.thx

    — answered July 29th 2011 by Rajaa Samaha
    permalink
    1 Comment
    • are you trying to connect with internet or localhost computer…if localhost then put http://10.0.2.2 instead of localhost….it will work fine

      — commented March 23rd 2012 by arun gupta
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.