Saturday, March 24, 2012

Pass variables to database

I have a bunch of code written by another developer. It is all in java script. The user walks through a series of instructions then answers a few questions. The java script tracks the answers, I believe they are using a cookie. At the end of the quiz they receive a certificate of completion that says Congrats (username) you got (##) out of (##) correct, etc. Each (somethinghere) being a variable. The javascript just does a document.write(something.parent);
I want to take what is in the variable and pass it to my SQL Database. I have written code in asp.net that does this but the code uses a text box and you have to put somthing in the text box then click Add and it will pass the variables on to my database. How do I do this with this javascript stuff?

Passing values from javascript to the server-side ASP code can be done by placing the value in a hidden text box, or any control which has a text property.

<SCRIPT Language="JavaScript">
document.Form1.TextBoxName.value = javascript variable
</SCRIPT>
You can retrieve the value on the server-side usingRequest.Form("TextBoxName")when the form is posted back.

hope this helps,
sivilian


Not sure where the final value you need is being displayed. You said a document.write?
Then, you may also want to put that var in a hidden html field, then, in your code behind, find the value...

string myVar = (HtmlInputHidden)(this.FindControl("myHiddenFieldName")).Value;


Hope it helps,
Zath


It seems like you want this to automagically happen, without using a text box and clicking a button.
I am assuming that you are not overly knowledgeable with web based development. So you need to know that, in general, to get info into the database the user has to do an action and post the data back to the server. This would be usually done with a form and a button. as Zath points out you can use a hidden control instead of a textbox. Your javascript that does the document.write could then call document.forms[0].submit() to do the post back to the server. This will refresh the page.
Alternatively, you could look at something fancy like AJAX or Javascript RPC (basically the same things). This is pretty advanced stuff.


Here is an example of my asp page:

<%@.ImportNamespace="System.Data.SqlClient" %>
<%@.ImportNamespace="System.Data" %>
<%@.PageLanguage="VB"Debug="TRUE" %>
<%
Dim conMMTMPAs SqlConnection
Dim strInsertAsString
Dim cmdInsertAs SqlCommand

conMMTMP =New SqlConnection("server=servername;UID=ID;PWD=password;database=DBname")
strInsert ="Insert Into SuperTrain (User,Score) Values ('" & Request.Querystring("User") &"','" & Request.Querystring("Score") &"')"
cmdInsert =New SqlCommand( strInsert, conMMTMP )
conMMTMP.Open()
cmdInsert.ExecuteNonQuery()
conMMTMP.Close()
%>
so usually i would pass something say to the url:http://www.mysite.com/page.aspx?User=Tester&Score=100
bam i have a new record in my database. I also have a page written like this:

<%@.ImportNamespace="System.Data.OleDb" %>
<%@.ImportNamespace="System.Data" %>
<%@.PageLanguage="VB"Debug="true" %>
<ScriptRunat="Server">

Sub Button_Click( sAsObject, eAs EventArgs )
Dim conAuthorsAs OleDbConnection
Dim strInsertAsString
Dim cmdInsertAs OleDbCommand
Dim strFirstNameAsString
conAuthors =New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" & Server.Mappath("database.mdb"))
strInsert ="Insert Into tblTest ( au_lname ) Values ( @.FirstName )"
cmdInsert =New OleDbCommand( strInsert, conAuthors )
cmdInsert.Parameters.Add("@.Firstname", txtProductName.Text)
conAuthors.Open()
cmdInsert.ExecuteNonQuery()
conAuthors.Close()
EndSub

</Script>
<html>
<head><title>Insert Parameters</title></head>
<body>
<formRunat="Server">
<h2>Add new Name</h2>
<b>First Name:</b>
<br>
<asp:TextBoxID="txtProductname"Runat="Server"/>
<p>
<asp:ButtonText="Add!"OnClick="Button_Click"Runat="Server"/>
</form>
</body>
</html>
now... I have a page not written by me that is in javascript and I need to pass the variables to an asp page or something so I can get them into my database:
here is part of the page, how do i get this javasrcipt variables into my pages?

<p><b><i><font face="Arial,Helvetica"><font size=+4> <!--RG_TITLE0--><SCRIPT LANGUAGE="JavaScript"><!--

if (parent.pass == 1)

{

document.write(parent.master_title);

}

else

{

document.write(parent.comp_title);

}

//-->

</SCRIPT><!--RG_TITLE1--> </font></font></i></b></center>

<p><br>

<center><table WIDTH="70%" >

<tr>

<td><img height=339

SRC="ribbon.png" height=399 width=159></td>

<td><i><font face="Arial,Helvetica">This certifies that</font></i>

<br>

<p><b><u><font face="Arial,Helvetica"><font size=+2> <!--RG_STUDENT_ID0--><SCRIPT LANGUAGE="JavaScript"><!--

document.write(parent.student_id);

//--></SCRIPT><!--RG_STUDENT_ID1--> </font></font></u></b>

<br>

<p><i><font face="Arial,Helvetica"> <!--RG_ACTION0--><SCRIPT LANGUAGE="JavaScript"><!--

var pass=parent.checkScores();

if (pass == 1){document.write(parent.master_act);

}

else

{document.write(parent.comp_act);

}

//--></SCRIPT><!--RG_ACTION1--> <b> <!--RG_COURSE_NAME_OR_TITLE0-->

0 comments:

Post a Comment