I am working on a rating tool.The tool consists of 5 images (5-star rating) on clixk of the image i want to update my rate table (i.e on each click i incerement the count in the database).Currently i am trying out using ajax to run carry the rate value to my serverside code.
my test.aspx is the following
<script type="text/javascript" >
function ratemystar(s)
{
if (s == 1)
{
var img1 = document.getElementById("img1")
img1.src ="images/OneStar.gif"
dorender(1);
}
if (s==2)
{
var img1 = document.getElementById('img1')
img1.src ="images/OneStar.gif"
var img2 = document.getElementById('img2')
img2.src ="images/OneStar.gif"
dorender(2);
}
if (s==3)
{
var img1 = document.getElementById('img1')
img1.src ="images/OneStar.gif"
var img2 = document.getElementById('img2')
img2.src ="images/OneStar.gif"
var img3 = document.getElementById('img3')
img3.src ="images/OneStar.gif"
dorender(3);
}
if (s==4)
{
var img1 = document.getElementById('img1')
img1.src ="images/OneStar.gif"
var img2 = document.getElementById('img2')
img2.src ="images/OneStar.gif"
var img3 = document.getElementById('img3')
img3.src ="images/OneStar.gif"
var img4 = document.getElementById('img4')
img4.src ="images/OneStar.gif"
dorender(4);
}
if (s==5)
{
var img1 = document.getElementById('img1')
img1.src ="images/OneStar.gif"
var img2 = document.getElementById('img2')
img2.src ="images/OneStar.gif"
var img3 = document.getElementById('img3')
img3.src ="images/OneStar.gif"
var img4 = document.getElementById('img4')
img4.src ="images/OneStar.gif"
var img5 = document.getElementById('img5')
img5.src ="images/OneStar.gif"
dorender(5);
}
var myurl = "/mytest.aspx?n=1"
function dorender(n)
{
http.open("GET", myurl , true);
http.onreadystatechange = handoverHttpResponse;
http.send(null);
}
function handoverHttpResponse()
{
if (http.readyState == 4)
{
if(http.status==200)
{
var results=http.responseText;l
document.getElementById('check_email').innerHTML = results;
}
}
}
function getHTTPObject()
{
var xmlhttp;
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
if (!xmlhttp)
{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
}
return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object
}
function unratenow(n)
{
var myimage = document.getElementById(n)
myimage.src ="images/OneStarDisabled.gif"
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Article Rating Tool</h1>
<img id="img1" src="images/OneStarDisabled.gif" title="i don seem to like it!" alt="i don seem to like it!" runat="server" style="width: 20px" onmouseover="unratenow(this.id);" onclick="ratemystar(1);"/>
<img id="img2" src="images/OneStarDisabled.gif" title="i find it ok!" alt="i find it ok!" runat="server" style="width: 19px" onmouseover="unratenow(this.id);" onclick="ratemystar(2);"/>
<img id="img3" src="images/OneStarDisabled.gif" title="i find it fine!" alt="i find it fine!" runat= "server" style="width: 19px" onmouseover="unratenow(this.id);" onclick="ratemystar(3);" />
<img id="img4" src="images/OneStarDisabled.gif" title="i find it very good!" alt="i find it very good!" runat="server" style="width: 18px" onmouseover="unratenow(this.id);" onclick="ratemystar(4);"/>
<img id="img5" src="images/OneStarDisabled.gif" title="i find it outstanding!" alt="i find it outstanding!" runat="server" style="width: 20px" onmouseover="unratenow(this.id);" onclick="ratemystar(5);"/>
<br />
</div>
<div id="check_email">
</div>
</form>
</body>
</html>
the serverside code lies in another file name mytest.aspx
<script runat="server" >
Dim conn As New vbz.connection
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' Dim id as Integer = Request.QueryString("id")
Dim sID As Integer = Request.QueryString("n")
'Dim ad As New Data.SqlClient.SqlDataAdapter(strSql, conn.getConnectionstring())
Dim SqlP(3) as SqlParameter
SqlP(0) = New Data.SqlClient.SqlParameter("@aid", 1)
SqlP(1) = New SqlClient.SqlParameter("@cid", 1)
SqlP(2) = New SqlClient.SqlParameter("@rate",sID)
SqlP(3) = New SqlClient.SqlParameter("@rowaffected", SqlDbType.Int)
SqlP(3).Direction = ParameterDirection.Output
SqlHelper.ExecuteNonQuery (conn.getConnectionstring, CommandType.StoredProcedure, "sp_rate_article", SqlP)
Dim ret_affected as Integer = Convert.ToInt32(SqlP(3).Value)
If ret_affected <> 0 Then
Response.Write ("Content is rated" & ret_affected )
Else
Response.Write ("Content is not rated")
End If
End Sub
</script>