Jump to content
xisto Community
mrdee

Accessing Mysql From Javascript Javascript to communicate with database

Recommended Posts

I found a nice program which allows you to put quizzes on your web site.You can design your own questions, set the layout of your page, and there is also a possibility of having the results emailed to you.It further offers a facility to store the results and all that, all you have to do is put a link in the program, telling it which page takes care of that.However, when I look at the HTML code the program generates, I see that most of it is written in Javascript.Obviously, it also uses variable names, but as I don't know any Java, I was wondering if those variables are exchangeable with PHP, in order to let it store the name of the quizzer and his/her results in a MySQL database, or whether Javascript can address the issue directly.If this question is not clear enough or if people require more information in order to answer the question, I can always post the HTML code of the page here.

Notice from jlhaslip:
Edit title to add javascript

Share this post


Link to post
Share on other sites

Java and javascript are two different programming languages.javascript is a client-side scripting language and Java is a full programming language capable of much more.Please confirm which one you are discussing.javascript is used in AJAX to connect to databases and provide interactive web pages. I am just about to learn some Ajax, so I can't provide any more info than this.

Share this post


Link to post
Share on other sites

Well, yes, sorry about the confusion.

It is Javascript of course.

The tags say

<script language="JavaScript">

.

 

Just to see if it would help, or ring any bells, here is the code for the entire page (a whole, but short quiz with 5 questions):

 

<HTML>

<HEAD>

<TITLE>Vlaanderen</TITLE>

<META NAME="Generator" CONTENT="SolidSoftware's KwizMaker v1.0 ?2002">

<META NAME="Author" CONTENT="">

<script language="JavaScript" src="clock.js">

 

 

</script>

 

 

<script language="JavaScript">

<!--

document.oncontextmenu=new Function("return false");

 

 

function login(){

document.location = "http://www.vlaanderen-flanders.org.uk/;; }

 

 

var ans = new Array;

var yourtry = new Array;

var thisscore = 0;

var x;

var secsAllowed=15;

var count=5;

var choice=3;

var username;

var email;

 

 

ans[1]="A";

ans[2]="C";

ans[3]="B";

ans[4]="C";

ans[5]="C";

 

 

for (x=1;x<=count;x++) yourtry[x]="";

 

 

function storetry(question, answer){

yourtry[question] = answer;

}

 

 

function getscore(){

var y;

thisscore = 0;

setCookie('save', 'false', 365);

for (y=1;y<=count;y++)

{

if (yourtry[y] =="")

{

alert("Please answer ALL questions");

return(false);

}

else if (yourtry[y] == ans[y]) thisscore++;

}

alert("Your Score is: " + Math.round(thisscore/count*100)+"%");

document.quizform.score.value = Math.round(thisscore/count*100)+"%";

document.quizform.name.value = username;

document.quizform.email.value = email;

document.quizform.lapsed.value = lapsedtimestr;

if (thisscore<count)showerrors();

return(true);

}

 

 

function showerrors(){

var h;

var v =145+((count-thisscore)*20);

h = "top=0,left=0,width=180,";

h = h + "height=" + v;

h = h + ",scrollbars=1";

var i;

var newwin;

newwin = window.open("", "", h);

newwin.document.write("<FONT size=4><B>Your Error(s)...</B><BR><BR>");

for(i = 1; i<= count;i++){

if (yourtry != ans){

newwin.document.write("\nQuestion: " + i);

newwin.document.write("<BR>");

}

}

newwin.document.write("<BR><B>Have another try...</B><BR><BR></FONT>");

newwin.document.write('<BUTTON onclick="window.close()">Close</BUTTON>');

}

 

 

function refresh(){

document.location.reload();

document.quizform.reset();

}

 

 

function getCookie(NameOfCookie)

{ if (document.cookie.length > 0)

{ begin = document.cookie.indexOf(NameOfCookie+"=");

if (begin != -1)

{ begin += NameOfCookie.length+1;

end = document.cookie.indexOf(";", begin);

if (end == -1) end = document.cookie.length;

return unescape(document.cookie.substring(begin, end)); }

}

return null;

}

 

 

function savequiz(){

setCookie('quizuser',document.quizform.name.value,365);

setCookie('QuizID',document.quizform.ID.value,365);

setCookie('save', 'true', 365);

for (x=1; x<=count;x++) setCookie('q'+x, yourtry[x], 365);

}

 

 

function setCookie(NameOfCookie, value, expiredays){

var ExpireDate = new Date ();

ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

document.cookie = NameOfCookie + "=" + escape(value) +

((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());

}

 

 

function docookies(){

var con;

username=getCookie('username');

email=getCookie('email');

if (username==null) {

alert('Please log in');

window.location = "http://www.vlaanderen-flanders.org.uk/;;

}

else {

document.quizform.name.value = username;

if ((getCookie('QuizID') == document.quizform.ID.value) && (getCookie('save')== 'true') && (getCookie('quizuser') == document.quizform.name.value))

{

con = confirm('Saved quiz found. OK to continue, Cancel to restart.');

if (con) getsave();

}

}

}

 

 

function getsave() {

var c;

var found = false;

var y = 0;

var ch = 0;

for(x=1; x<=count;x++) {

found = false;

c = getCookie('q'+x);

yourtry[x] = c;

while(found ==false){

if ((document.quizform.elements[y].value == c) && (document.quizform.elements[y].type == "radio"))

{

document.quizform.elements[y].checked = true;

found = true;

}

y=y+1;

ch=ch+1;

}

y = y + choice-ch;

ch=0;

}

}

 

 

//-->

</SCRIPT>

</HEAD>

<BODY onload="docookies();startclock(); checkLocation()" onUnload="refresh()" BGCOLOR="#FFFFFF" TEXT="#FFFFFF" BGPROPERTIES="fixed" >

 

 

<DIV ID="object1"

STYLE="position:relative; visibility:show;

left:85%; top:0px; z-index:2">

<TABLE BORDER="4" BGCOLOR="#000000" CELLPADDING="0" CELLSPACING="0">

<TR><TD VALIGN="top" ALIGN="left" ><img src="0fdg.gif" name=min ><img src="0fdg.gif" name=minu><img src="cfdg.gif" name=gap ><img src="0fdg.gif" name=sec ><img src="0fdg.gif" name=secu ></TD></TR>

</TABLE>

</DIV>

 

 

<H1 ALIGN=CENTER>

<FONT FACE="sans-serif" SIZE="6" COLOR="#000000">

 

Vlaanderen

</FONT>

</H1>

<HR>

<FORM NAME=quizform method="POST" action="http://forums.xisto.com/no_longer_exists/; onSubmit="return getscore()">

<P align="LEFT">

<FONT FACE="serif" SIZE="3" COLOR="#FFFFFF">

 

<B> Question 1: Wie was eerste minister in 1978?</B><BR><BR>

</FONT>

<FONT FACE="monospace" SIZE="3" COLOR="#FFFFFF">

 

a: <input type=radio value=A name=q1 onclick="storetry(1, this.value)">Wilfried Martens<BR>

</FONT>

<FONT FACE="monospace" SIZE="3" COLOR="#FFFFFF">

 

b: <input type=radio value=B name=q1 onclick="storetry(1, this.value)">Hugo Schiltz<BR>

</FONT>

<FONT FACE="monospace" SIZE="3" COLOR="#FFFFFF">

 

c: <input type=radio value=C name=q1 onclick="storetry(1, this.value)">Leo Tindemans<BR>

</FONT>

<BR><BR><HR width="50%"><BR>

</p>

<P align="LEFT">

<FONT FACE="serif" SIZE="3" COLOR="#FFFFFF">

 

<B> Question 2: Wat is de echte naam van 'Madame Non'?</B><BR><BR>

</FONT>

<FONT FACE="monospace" SIZE="3" COLOR="#FFFFFF">

 

a: <input type=radio value=A name=q2 onclick="storetry(2, this.value)">Noella Millequet<BR>

</FONT>

<FONT FACE="monospace" SIZE="3" COLOR="#FFFFFF">

 

b: <input type=radio value=B name=q2 onclick="storetry(2, this.value)">Joelle Milliquet<BR>

</FONT>

<FONT FACE="monospace" SIZE="3" COLOR="#FFFFFF">

 

c: <input type=radio value=C name=q2 onclick="storetry(2, this.value)">Joelle Milquet<BR>

</FONT>

<BR><BR><HR width="50%"><BR>

</p>

<P align="LEFT">

<FONT FACE="serif" SIZE="3" COLOR="#FFFFFF">

 

<B> Question 3: Wie is de onlangs mislukte formateur?</B><BR><BR>

</FONT>

<FONT FACE="monospace" SIZE="3" COLOR="#FFFFFF">

 

a: <input type=radio value=A name=q3 onclick="storetry(3, this.value)">Didier Reynders<BR>

</FONT>

<FONT FACE="monospace" SIZE="3" COLOR="#FFFFFF">

 

b: <input type=radio value=B name=q3 onclick="storetry(3, this.value)">Yves Leterme<BR>

</FONT>

<FONT FACE="monospace" SIZE="3" COLOR="#FFFFFF">

 

c: <input type=radio value=C name=q3 onclick="storetry(3, this.value)">Olivier Maingain<BR>

</FONT>

<BR><BR><HR width="50%"><BR>

</p>

<P align="LEFT">

<FONT FACE="serif" SIZE="3" COLOR="#FFFFFF">

 

<B> Question 4: Wanneer was de Guldensporenslag?</B><BR><BR>

</FONT>

<FONT FACE="monospace" SIZE="3" COLOR="#FFFFFF">

 

a: <input type=radio value=A name=q4 onclick="storetry(4, this.value)">11/8/1300<BR>

</FONT>

<FONT FACE="monospace" SIZE="3" COLOR="#FFFFFF">

 

b: <input type=radio value=B name=q4 onclick="storetry(4, this.value)">11/7/1305<BR>

</FONT>

<FONT FACE="monospace" SIZE="3" COLOR="#FFFFFF">

 

c: <input type=radio value=C name=q4 onclick="storetry(4, this.value)">11/7/1302<BR>

</FONT>

<BR><BR><HR width="50%"><BR>

</p>

<P align="LEFT">

<FONT FACE="serif" SIZE="3" COLOR="#FFFFFF">

 

<B> Question 5: Wanneer werd de Gentse universiteit vervlaamst?</B><BR><BR>

</FONT>

<FONT FACE="monospace" SIZE="3" COLOR="#FFFFFF">

 

a: <input type=radio value=A name=q5 onclick="storetry(5, this.value)">1900<BR>

</FONT>

<FONT FACE="monospace" SIZE="3" COLOR="#FFFFFF">

 

b: <input type=radio value=B name=q5 onclick="storetry(5, this.value)">1940<BR>

</FONT>

<FONT FACE="monospace" SIZE="3" COLOR="#FFFFFF">

 

c: <input type=radio value=C name=q5 onclick="storetry(5, this.value)">1930<BR>

</FONT>

<BR><BR>

</p>

<HR>

<BR>

<FONT size=+1>If you are not  

<A NAME="stayput"></A>

<input type=text size=30 value="" name="name">   please click <A HREF="#stayput" onClick="login()">here</A>.<BR><BR></Font>

<FONT size=+1>Your Score:   <font>

<input type=hidden name="email" value="">

<input type=hidden name="URL" value="http://forums.xisto.com/no_longer_exists/;

<input type=hidden name=ID value=Nr.1>

<input type=hidden name="lapsed" value="">

<input type=text size=5 value="N/A" name=score><BR><BR>

<input type=submit value="Check Score" >  

<input type=reset value=Clear All onClick="refresh()">  

<input type=button value=" Close This Window " onclick="self.close()">

</FORM>

<HR>

</BODY>

</HTML>

Hope that helps. Edited by mrdee (see edit history)

Share this post


Link to post
Share on other sites

sorry i did not read your javascript code. I usually don't read long lines of code until I'm fully awake, bored or really have to. But from what you said you want to store variables from javascript to mysql database? well only way to do that is pass javascript variables to PHP. Thats the quick answer. method 1. post javascript variables using 'get' or 'post' and grabs those in PHP then do mysql queries.method 2. post javascript variables using ajax (post or get) and do the same thing in PHP and Mysql.method 1 is more simpler if you are not familiar with ajax just yet.

Share this post


Link to post
Share on other sites

He's not asking how to do it, from his question, I think he is just wondering if that code IS posting to sql. I do not see any ajax in that code. All of the values are being stored in cookies not sql. If you want to modify that sonesay has some great options for you.

Share this post


Link to post
Share on other sites
Struck in Ajax.Accessing Mysql From Javascript

hi to all

lest quickly get back to my question.

I have 2 pages namely 1.Jsp and 2.Jsp

1.Jsp contains few checkboxes.

and based on the checkboxes that are checked 2.Jsp creates corresponding textboxes.

I.E if in 1.Jsp out of 5 checkboxes only 2 are checked then in 2.Jsp there will be 2 textboxex.

Also in 2.Jsp goes my code to connect to mysql and retrieve some information from database and populate the corresponding checkbox.Also in 2.Jsp I have next button which account for next record.

how to I proceed...

I used ajax for coonecting to datbase but cant use it since javascript is client side.

so help me asap!!

Thanks

Respect Women

warm regards

op

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.