Results 1 to 10 of 10
  1. twbbas's Avatar
    Posts
    87 Posts
    Global Posts
    99 Global Posts
       #1  
    Hi, I am always looking at precentral's forums but have never posted until now. I'm trying to make a few simple apps that just connect to my PHP file on my server and display some information from that page.

    I'm a semi-experienced PHP programmer so that's why I want to do most of the work in PHP. I've googled and googled looking for a way to just connect to a website and display information but I can't find anything. Some people say to use ajax (which I can't get to work) and others say it can't be done in javascript (but I know it can be for webOS).

    Any developers out there mind answering my simple question? I'm very new to javascript but I do know html. Thanks.
  2. #2  
    The way I do it is with XMLHttpRequest. But yes, you can also do it with an Ajax request.
    Quick Post: The quick way to post messages and photos to Twitter & Facebook (video link)
    Music Player (Remix): The next generation music listening experience on webOS (video link)
    GeoStrings: Set location-based reminders and never forget another task (video link)

    Twitter: @Hedami
  3. #3  
    I'm not sure which part is your question, but if you want to use ajax this is helpful: Prototype JavaScript framework: Introduction to Ajax

    I don't personally work with php for this purpose, ajax is much nicer with a restful webservice, but Dan's way is probably the best starting point.
  4. twbbas's Avatar
    Posts
    87 Posts
    Global Posts
    99 Global Posts
       #4  
    Hey another really simple question...

    Can any of you post a short example of how to use it? Right now I'm doing

    Code:
    new Ajax.Request('LINK',
    {
        method:'get',
        onSuccess: function(transport){
          var response = transport.responseText || "no response text";
          alert("Success! \n\n" + response);
        },
        onFailure: function(){ alert('Something went wrong...') }
      });
    and I don't know what to do after that. I want it to change a label's text to what it gets from the website. Thanks.
  5. #5  
    $("myLabelId").innerText = response;
  6. mosdl's Avatar
    Posts
    781 Posts
    Global Posts
    787 Global Posts
    #6  
    There is no alert() in webos.

    You probably want to use Mojo.Log.info(), but for that you need to use palm-log.bat/sh from the sdk
    Apps: MyQ for Netflix (Phone/TouchPad), Giantbomb (Phone), Excavate (Reddit/Digg clients for TouchPad)
  7. twbbas's Avatar
    Posts
    87 Posts
    Global Posts
    99 Global Posts
       #7  
    Quote Originally Posted by roar View Post
    $("myLabelId").innerText = response;
    I can't seem to get that to work even when replacing response with "text".

    I've tried it right after the button tap event and it doesn't do anything.
  8. twbbas's Avatar
    Posts
    87 Posts
    Global Posts
    99 Global Posts
       #8  
    Quote Originally Posted by mosdl View Post
    There is no alert() in webos.

    You probably want to use Mojo.Log.info(), but for that you need to use palm-log.bat/sh from the sdk
    Yea I figured webos didn't have it but it doesn't matter because it was from an ajax tutorial and I just wanted to make it edit the label text. Thanks though.
  9. twbbas's Avatar
    Posts
    87 Posts
    Global Posts
    99 Global Posts
       #9  
    Quote Originally Posted by mosdl View Post
    There is no alert() in webos.

    You probably want to use Mojo.Log.info(), but for that you need to use palm-log.bat/sh from the sdk
    Yea I figured webos didn't have it when I didn't see any alert pop up. All I did was copy from an ajax tutorial and my goal right now is to just get that script to read from a website then display the info in a label field. Thanks though.
  10. #10  
    I'm a PHP dev as well, and I built my first app with the same thing you have in mind. It's like a database driven website, only you're replacing the browser window with an app. I'm using AJAX to send info to my PHP script, which in turn queries my DB and returns the info in JSON using PHP's excellent json_encode() function. If you are good on the PHP side I'll post a quick example of the webOS code that I use as a login for my second app...

    This example assumes a username field, password field, and button are correctly setup in your app already. The initial AJAX request is located in my login button handler:
    Code:
    LoginAssistant.prototype.loginHandler = function(event)
    {
        // CHECK USERNAME & PASSWORD
        // MAKE AJAX LOGIN REQUEST
    
            if (this.usernameModel['value'] == '' || this.passwordModel['value'] == '')
            {
                Mojo.Controller.errorDialog('Please enter both your Username and Password!', this.controller.window);
                this.controller.get('login').mojo.deactivate();
            }
            else
            {
                // SET AJAX URL VARIABLE
                // CREATE HASH OF VALUES TO PASS TO WEB SERVER
    
                    var loginAjaxURL = 'http://www.yourwebsite.com/yourscript.php';
    
                    loginAjaxParameters = new Hash();
                    loginAjaxParameters.set('username', this.usernameModel['value']);
                    loginAjaxParameters.set('password', this.passwordModel['value']);
    
                // SET AJAX ATTRIBUTES
                // MAKE AJAX REQUEST
    
                    var loginAjaxAttributes =
                    {
                            method: 'post',
                          evalJSON: 'true',
                        parameters: loginAjaxParameters,
                         onSuccess: this.loginAjaxSuccess.bind(this),
                         onFailure: this.loginAjaxFailure.bind(this)
                    };
    
                    var loginAjax = new Ajax.Request(loginAjaxURL, loginAjaxAttributes);
            }
    }
    Your PHP script can access the data sent as $_POST variables. Do some error checking on the data, query your database, put the results in an array, then echo the results in JSON (ie. echo json_encode($results_array);). Next, setup the handler for the AJAX request that processes the JSON data that was returned from the PHP script (Take note of the "onSuccess" property in the AJAX request above):
    Code:
    LoginAssistant.prototype.loginAjaxSuccess = function(response)
    {
        // GET DATA FROM JSON RESPONSE
        // TAKE ACTION BASED ON THE RESPONSE "STATUS"
    
            response = response.responseText.evalJSON(true);
    
            if (typeof(response.status) == 'undefined') {Mojo.Controller.errorDialog('There was an error logging into your account, please check your ionternet connection try again!', this.controller.window); this.controller.get('login').mojo.deactivate();}
            else
            {
                // SET LOCAL VARIABLES
                // DEACTIVATE BUTTON
                // PUSH NEXT SCENE BASED ON USER TYPE
    
                    var userID        = response.userID;
                    var userFirstName = response.userFirstName;
                    var userLastName  = response.userLastName;
    
                    this.controller.get('login').mojo.deactivate();
                    this.controller.stageController.swapScene('yourScene');}
            }
    }
    NOTE: The if statement above that handles errors has two commands on one line, be sure to edit appropriately! Also, "status" is one of the variables I send back, that isn't built into the response natively!!!

    I stripped out some stuff in the example to make it as simple as possible (mainly code that handles error messages I send back to the app from PHP). This example should get you up and running though. Add in whatever you need! Let me know if you have any questions...
    Last edited by Laxidasical; 02/18/2010 at 01:48 PM. Reason: Fixed Typo
    MoBill - Use your Authorize.net account to bill your customers with your webOS device!!
    MoJack - Track your lost or stolen webOS device from anywhere!
    Time to get VIRAL

Posting Permissions