Welcome!

Welcome to the Official BlackBerry Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Web and WebWorks Development

Reply
Trusted Contributor
sabanim
Posts: 103
Registered: ‎03-31-2011
My Carrier: Verizon

How do I access MySql data from my webwords widget?

Hello all,

 

I am new to webworks but willing to learn :smileytongue:

 

I have a widget that needs to pull some data (text and a pic.) from a Mysql data base over the web.

I have no clue where to start. From what I have read you need to use php json and/or ajax Ok thats nice and all but....I dont how.

 

So Im looking either for a tutorial or sample code with comments or anything else an I'll attempt to sort it out.

 

My Project:   Using plug in for Visual Studio

                       Buttons will access the database and will populate an img tag and/or textbox tag

                             if no internet connection will store the information it previous received.

                       Right now my buttons code behind is jscript.

                       I do not need to write data or sort just pull the data down any thing else is just "gravy" :Shifty:

 

Thanks for helping

 

Please use plain text.
New Developer
MaxxJag
Posts: 78
Registered: ‎04-17-2011
My Carrier: Bell

Re: How do I access MySql data from my webwords widget?

My app uses AJAX to get PHP to access the MySQL data. a simple | delimited response.

Otherwise there's the WebDB you can  use on the playbook (and BB OS6 I think)

WebDB uses SQL-like syntax, of course you'd need to create a javascript page to create the DB

MaxxWares.com
Current apps:
-BBOS 10-
MaxxTransit - Coming soon
Please use plain text.
Trusted Contributor
sabanim
Posts: 103
Registered: ‎03-31-2011
My Carrier: Verizon

Re: How do I access MySql data from my webwords widget?

OK so the Ajax is part of the widget.  Any pointers to a good guild on how that is done?

Please use plain text.
New Developer
MaxxJag
Posts: 78
Registered: ‎04-17-2011
My Carrier: Bell

Re: How do I access MySql data from my webwords widget?

Not sure of any guides. I use w3schools.com for the basic ajax syntax. You don't need the cross browser stuff since it's supposed to all be the same for webworks.

 

For how you parse the data, it's up to you. I use | delimited and extract it on the javascript side. I use that character since it's one of the least likely used.

MaxxWares.com
Current apps:
-BBOS 10-
MaxxTransit - Coming soon
Please use plain text.
Trusted Contributor
sabanim
Posts: 103
Registered: ‎03-31-2011
My Carrier: Verizon

Re: How do I access MySql data from my webwords widget?

Please take a look at this code:

What part of the red code do you need for the blackberry browser and I don't reconize where it is stating the http to connect to.  This is from the link you suggested w3school.com-- http://www.w3schools.com/PHP/php_ajax_database.asp

 

Thanks for helping this newbie out.

 

function showUser(str)

{

if (str=="")

  {

  document.getElementById("txtHint").innerHTML="";

  return;

  }

if (window.XMLHttpRequest)

  {// code for IE7+, Firefox, Chrome, Opera, Safari

  xmlhttp=new XMLHttpRequest();

  }

else

  {// code for IE6, IE5

  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

  }


xmlhttp.onreadystatechange=function()

  {

  if (xmlhttp.readyState==4 && xmlhttp.status==200)

    {

    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;

    }

  }

xmlhttp.open("GET","getuser.php?q="+str,true);

xmlhttp.send();

}  
Please use plain text.
New Developer
MaxxJag
Posts: 78
Registered: ‎04-17-2011
My Carrier: Bell

Re: How do I access MySql data from my webwords widget?

 

function showUser(str)
{
  if (str=="") {
    document.getElementById("txtHint").innerHTML="";
    return;
  } 
  xmlhttp=new XMLHttpRequest();

  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
  //Change getuser.php to http://www.wateva.com/getstuff.php
  xmlhttp.open("GET","getuser.php?q="+str,true);

  xmlhttp.send();
}  

 Removed the condition for IE7/IE6 etc since BB uses the current standard for AJAX.

Also, the PHP/ASP page might require a special header for cross domain permissions. Took me a while to figure it out.

Not at home so don't have access to that, but I'll try to send it as soon as I can. Otherwise google for security cross domain header or something

 

MaxxWares.com
Current apps:
-BBOS 10-
MaxxTransit - Coming soon
Please use plain text.
Trusted Contributor
sabanim
Posts: 103
Registered: ‎03-31-2011
My Carrier: Verizon

Re: How do I access MySql data from my webwords widget?

awsome thanks for the help so far

Please use plain text.
Trusted Contributor
sabanim
Posts: 103
Registered: ‎03-31-2011
My Carrier: Verizon

Re: How do I access MySql data from my webwords widget?

ok so here is the code im using to populate a div tag in a webworks html form.

 

But its not working in the simulator or in firefox.  Just nothing happens on the button click not even a crash.

 

Anyone see what I'm doing wrong?

 

SCRIPT

function Button2_onclick(str) {
if (str=="") {
    document.getElementById("txtHint").innerHTML="";
    return;
  }
  if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
 
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
  //Change getuser.php to http://www.wateva.com/getstuff.php
  xmlhttp.open("GET","http://ishopstark.com/mobileapp.php?category=1"+str,true);

  xmlhttp.send();
}


PHP PAGE

<?php
$app = $_REQUEST['appid'];
$cat = $_REQUEST['category'];
switch ($cat){
    case 1 :
        echo "<p>category 1</p>";
        echo "<img src='/images/bannerads/hales_bannerad.gif'><br>
       
        <a href=\"mobileapp.php?category=2\">Click here to test and see if once your page is loaded it will jump from link to link inside the app on this same page</a>";

        echo"<p>Lorem ipsum dolor sit amet, consectetur  </p>";
        break;

        case 2 :
        echo "<p>category 2</p>";
        echo "<img src='/images/bannerads/lehr_bannerad.gif'><br>";
        echo"<p>Lorem ipsum dolor sit amet, consectetur  </p>";
        break;

        default :
        echo "<p>Default</p>";
        echo"<p>Lorem ipsum dolor sit amet, consectetur  </p>";
        break;
}
?>

Please use plain text.
New Developer
pnewhook
Posts: 70
Registered: ‎01-13-2011
My Carrier: Rogers

Re: How do I access MySql data from my webwords widget?

Where are you binding Button2_onclick to the button click event. If nothing's happening, it sounds like you haven't connected the two. try putting something simple like 

alert("Button Clicked");

 as the first line of your Button2_onclick function. Once we know everything's wired up properly it's easier to help.

 

Also, I don't want to discourage you from posting in the forums, or even web works development in general, but this is a pretty general question, so you may be better served by reading some JavaScript and Ajax tutorials before you go any further.

 

Please use plain text.
Trusted Contributor
sabanim
Posts: 103
Registered: ‎03-31-2011
My Carrier: Verizon

Re: How do I access MySql data from my webwords widget?

ok first off I am not passing anything to str so heres a fix I think will work.
function Button2_onclick(){
var category = "lunch";
getCategory(category);
}

function getCategory(str) { THIS BEING THE OLD BUTTON FUCNTION
if (str == "")
{document.getElementById("txtHint").innerHTML="";
Please use plain text.