I started building a simple taskwidget to try out jquery. It’s not much but a simple AJAX application using PHP and mysql on the backend. Here a some of the cool things I learned along the way.
A common gotcha with JavaScript is access to “this” inside callbacks and functions. For example, $.get has a callback but inside the callback “this” is out of scope. So if you need to use a function in the object that contains the $.get define a local variable and set the value equal to “this”. Here is a quick example of this scenario. Once you are done with the callback the local instance variable of “this” will get cleaned up by garbage collection.
var foo = this;
$.get("test.php", function(data){
//this is out of scope in this callback
alert("Data Loaded: " + data);
//but foo is now = this
foo.sendDataSomeWhereElse(data)
});
On the PHP backend I found a nice mysql wrapper class . It makes it simple to organize MySQL connection and queries. You can do things like this to connect to your database using a simple config file.
$db = new Database($config['server'], $config['user'], $config['pass'], $config['database'], $config['tablePrefix']);
Then a function would look like this:
$db->connect();
$sql = "insert into tmastertask (masterTaskDescription) VALUES ('" . $values[0] . "');";
$db->query($sql);
$jsonInsertArray[] = "success";
$jsonResultArray["InsertResult"]=$jsonInsertArray;
echo json_encode($jsonResultArray);
$db->close();

Posted in
Tags: