simple AHAH (Asynchronous HTML And HTTP) library

November 12, 2008 Leave a comment

Just thought of sharing a very simple javascript AHAH library and quickly demo a simple usage in one line of code. AHAH works very similarly to AJAX but without the X of Ajax. It unables you to dynamically update your page content without a whole page refresh. Instead of parsing XML data back you could simply return html. Here is how simple it is to dynamically reload your page content using AHAH.

File: ahahlib.js

Step1: Create a javascript file and copy the code in the following steps.

Step2: Write the following function to specify the XMLHTTPrequest object from different browsers

function getXMLHTTPreq(){
try {
req = new XMLHttpRequest(); /* Firefox */
} catch (e){
try{
req = new ActiveXObject(“Msxml2.XMLHTTP”);  /* previous versions of IE7 */
} catch (e) {
try {
req = new ActiveXObject(“Microsoft.XMLHTTP”); /* IE7+ */
} catch (err) {
req = false;
}
}
}
return req;
}

Step3: Create a function to write the return text to the designated document page element

var http = getXMLHTTPreq(); //XMLHTTPrequest object
function responseAHAH(pageElement) {
var output = ”;
if(http.readyState == 4){
if(http.status == 200){
output = http.responseText;
document.getElementById(pageElement).innerHTML = output;
}
}
}

Step4: Create the function that will be called from your html

/* call this AHAH function for dynamic html content display*/
function getAHAH(url, pageElement, LoadingMessage)
{
document.getElementById(pageElement).innerHTML = LoadingMessage;
http.onreadystatechange
= function(){responseAHAH(pageElement);};
http.open(“GET”, url, true);
http.send(null);

}

Example:

<script language=”Javascript” src=”ahahlib.js”></script>

<input type=”text” id=”mypage” name=”mypage” size=50>
<input type=”button” onClick=”getAHAH(document.getElementById(‘mypage’).value, ‘displaydiv’, ‘Loading content…’)” value=”Display page content”>
<br>
<div id=”displaydiv”></div>

In the above example, we have an input text to let you display whatever page you want to.

ahah

In displayTasks.php you may have simple echo strings to complex server side data thrown from database.

<?php
echo “Task1:<br><b>Configure SQL Server…<b>”;
?>

In step2, the xmlhttprequest object property readyState reports on the status of the request where

0=uninitialized, 1=loading, 2=loaded, 3=interactive, 4=completed

The status property returns the HTTP status code as 200 returns a successful request.

PHP and SQL Server Stored Procedures, how to insert a new record under 4 steps

November 11, 2008 3 comments

Having SQL Server storing data for your PHP web application isn’t a common combination but is a far better option than having MySQL when it comes to larger database. I wont brag about why choose SQL Server over MySQL instead will quickly demonstrate in few steps how to execute stored proc in PHP using mssql database extension library.

We will be using mssql_query to execute the stored procedure and return records added using SCOPE_IDENTITY(). Please accept my appologies for coding indentation and lack colouring! @@

Let’s take a task list scenario where we manage our to-do tasks in a table having an ID, name, date added, priority task, and active fields.

First step, create your table in SQL server like the following:

tbltask

Step 2, Create a stored procedure to insert records:

CREATE proc [dbo].[usp_InsertTask]
(
@TaskName varchar(255),
@TaskDateAdded datetime,
@TaskPriority int,
@TaskActive bit
)
as
insert into tblTask(TaskName, TaskDateAdded, TaskPriority, TaskActive)
values (@TaskName, @TaskDateAdded, @TaskPriority, @TaskActive)
SELECT SCOPE_IDENTITY() as id
RETURN

Note “SELECT SCOPE_IDENTITY() as id” will return the inserted record TaskID set as auto increment.

Check out Pinal Dave, SQL Server MVP, blog on the differenced between SCOPE_IDENTITY() vs @@IDENTITY vs IDENT_CURRENT to retrieve the last inserted record.

Step3, back to PHP to create your mssql_connect connection and execute stored procedure

<?php
$myServer = “10.64.0.7”;
$myUser = “mydevuser”;
$myPass = “mydevpassword”;
$myDB = “devdatabase”;

// connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die(“Couldn’t connect to SQL Server on $myServer”);

// select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die(“Couldn’t open database $myDB”);

//setup variable data
$TaskName = ‘Configure SQL Server’;
$TaskDateAdded = ’12/22/2008′;
$TaskPriority = 2;
$TaskActive = true;

$result = mssql_query(“usp_InsertTask ‘$TaskName’, ‘$TaskDateAdded’, ‘$TaskPriority’, ‘$TaskActive'”, $dbhandle);
OR
$result = mssql_query(“EXEC dbo.usp_InsertTask @TaskPriority = ‘$TaskPriority’, @TaskName = ‘$TaskName’, @TaskDateAdded = ‘$TaskDateAdded’, @TaskActive = ‘$TaskActive'”, $dbhandle);
$result = mssql_fetch_object($result);
echo “Record ID = “.$result->id;

// close the connection
mssql_close($dbhandle);
?>

Step 4, DONE!

Most blog posts found when google-ing php and stored procedures SQL Server will go on about using mssql_execute function instead of mssql_query directly. But you will find much more posts about how it actually doesn’t work as php.net/mssql manual suggested. Probably because of new version of SQL server? PHP? or wrong mssql driver or configuration?

The steps i have provided is working under PHP5 on a Debian dev server and SQL Server 2008 CTP.

Microsoft HTTPAPI/2.0 disabling Apache

November 7, 2008 209 comments

I’be been working on php for 2 months now and developing on my laptop with WAMP installed before uploading to work’s dev server. Now working with SQL Server instead of MySQL, I’ve installed SQL SERVER 2008 CTP for test and suddently Apache went down as port 80 was used by Microsoft HTTPAPI/2.0.

I then found SSRS (SQL Server Reporting Services) was still running after uninstalling SQL Server 2008 as it features a web service even though IIS is not installed according to wiki.

I couldn’t find any info on google relating to this small issue that puzzled me for a short while. i hope this helps finding ppl stupid like me sometimes to solve their problem

VS2008 RTM out!

November 22, 2007 Leave a comment

that’s right, 2008 is released and available for download via MSDN website. Ive been on beta2 for a while now, mainly taking advantage of the javascript intellisense and debugging, and playing a bit with LINQ.

checkout all the details from Scott Guthrie’s blog for new features quick tour, and also steps to uninstall beta 2

hop hop, jumping in my msdn subscription and start downloading..

Categories: .Net, Visual Studio

Let’s party at “Mix on Campus”

October 29, 2007 Leave a comment

mixoncampus.jpg 

“Mix on Campus” is coming to QUT; yet another great Microsoft event on campus. Microsoft Australia is inviting all tertiary students and lecturers from University and Tafe around Brisbane to come to this free web design and web developer event. It’s a great get together with fellow students and lecturers, learn the latest web trends and technologies, and hear from industry experts. Best of all, it’s FREE and straight off after exams!! Top that with some great give-away prices and it will be as awesome as Tech.Ed 2007 and Remix for academics. I’m not sure what technologies will be presented yet but expect a glance of Visual Studio 2008, Silverlight, and Expression.

Check out the official blog ,invite your facebook contacts! and register NOW!

Venue: QUT Gardens Point Campus, Room 117, B block, 2 George Street, Brisbane

Date and Time: 20/Nov 10am to 4pm

Registration: http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032356512&Culture=en-AU

it’s STILL not cheating

October 25, 2007 2 comments

Microsoft promotion “It’s not cheating” is back for university and Tafe students! Earlier this year, Microsoft launched this promotion to allow University and Tafe students to purchase the Ultimate version of Office 2007 for just $75. To be eligeable, students only need to provide a valid email address and currently enrolled at an educational institution. So hurry up if you have missed this offer earlier this year; offer ends May 15, 2008.

http://www.itsnotcheating.com.au/

newoffer.jpg

“Orcas” becomes Beta2

September 1, 2007 1 comment

 aa700831_vs08_ven-usmsdn_10.png 

The next Visual Studio is available now in Beta 2! Yey. Last July, Boon-Tiong Lim presented Visual Studio 2008 in its early release Beta 1 during the July QMSDNUG meeting. VS 2008 is improving technology developments such as Smart Client Application, MS Office application, Windows Vista application, and now handle data more productively, enable new web experience and improves application life cycle management. Check out the overview of Visual Studio 2008 white paper from here. So what’s new for us? .NET Framework 3.5, JavaScript Intellisense (very handy for AJAX developers), and LINQ (Language Integrated Query). Thanks to Marty, I got a few disc copies of Beta 2 but it can also be downloaded from msdn website.

The next MS campus activity @ QUT sometime next month will surely cover Visual Studio 2008 Beta 2 and possibly Sharepoint. The official Launch of Visual Studio wont come until February 27.

Categories: .Net, Visual Studio