Ajax + php + xml: Online Exam Q & A system

Source: Internet
Author: User
Tags visibility

<Html>
<Head>
<Title> Ajax application </title>
<Script language = "JavaScript" type = "text/javascript" src = "ajax. js"> </script>
<Style type = "text/css">
<! -- CSS style sheets are used to define the display appearance of a page and separate content from display.
# Answershow {border: # E2FAFA; color: #000099; font-size: 12px ;}
# Main {visibility: hidden ;}
# Begin {
Visibility: visible;
Position: absolute;
Left: 135px;
Top: 103px;
Width: 37px;
Height: 16px;
Z-index: 1;
}
-->
</Style>
</Head>
<Body>
<Div id = "begin"> <input type = "button" id = "startq" value = "start"> </div>
<H1> simple test system <Div id = "main">
<P> <B> answer: </B> <input type = "text" id = "answer">
<Input type = "button" value = "Submit" id = "submit">
<Br> <B> Tip: </B> <span id = "answershow"> </span>
</Div>
<Script language = "JavaScript" type = "text/javascript" src = "manage. js"> </script>
</Body>
</Html>

Ajax. js file

Var ajaxreq = false, ajaxCallback;
Function ajaxRequest (filename ){
Try {
Ajaxreq = new XMLHttpRequest ();
} Catch (error ){
Try {
Ajaxreq = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (error ){
Return false;
    }
   }
Ajaxreq. open ("GET", filename );
Ajaxreq. onreadystatechange = ajaxResponse;
Ajaxreq. send (null );
}
Function ajaxResponse (){
If (ajaxreq. readyState! = 4) return;
If (ajaxreq. status = 200 ){
If (ajaxCallback) ajaxCallback ();
} Else alert ("Request failed:" + ajaxreq. statusText );
Return true;
}
It is easy to create xmlhttp. The best method to create xmlhttp is more practical and sound.

Mange. js file,

Var qn = 0, questions, right = 0;
Function getQuestions () {// getElementById is used to locate the page element to be operated
Document. getElementById ("main"). style. visibility = "visible ";
Document. getElementById ("begin"). style. visibility = "hidden ";
Document. getElementById ("answer"). focus ();
Obj = document. getElementById ("question ");
Obj. firstChild. nodeValue = "Loading .....";
AjaxCallback = nextQuestion;
AjaxRequest ("questions. xml"); // XML text loading from the server
}
Function nextQuestion () {// display the next question
Questions = ajaxreq. responseXML. getElementsByTagName ("ask ");
Obj = document. getElementById ("question ");
If (qn <questions. length ){
Q = questions [qn]. firstChild. nodeValue;
Obj. firstChild. nodeValue = q;
} Else {// score when the user answers the question
Var anobj = document. getElementById ("answershow ");
Anobj. innerHTML = "your score is:" + right/questions. length * 100;
   }
}
Function checkAnswer () {// instantly checks user answers
Answers = ajaxreq. responseXML. getElementsByTagName ("key ");
A = answers [qn]. firstChild. nodeValue;
Answerfield = document. getElementById ("answer ");
If (answerfield. value = "") {var anobj = document. getElementById ("answershow"); // prompt when the user does not answer
Var anobj = document. getElementById ("answershow ");
Anobj. innerHTML = "Sorry, you have not answered ";
Answerfield. focus ();
Return ;}
Else if (a. toLowerCase () = (answerfield. value). toLowerCase () {// prompt when the user is correct
Right ++;
Var anobj = document. getElementById ("answershow ");
Anobj. innerHTML = "correct answer! ";
Answerfield. focus ();
   }
Else {
Var anobj = document. getElementById ("answershow"); // The prompt displayed when the user makes an error
Anobj. innerHTML = "incorrect answer. Answer:" +;
Answerfield. focus ();
    }
Qn = qn + 1;
Answerfield. value = "";
NextQuestion ();
}
// The following action buttons create Event Processing
Obj = document. getElementById ("startq ");
Obj. onclick = getQuestions;
Ans = document. getElementById ("submit ");
Ans. onclick = checkAnswer;
This file is an analysis node and determines whether the answer is correct.

Questions. php

<? Php
$ Link = mysql_connect ("localhost", "root ","");
Mysql_select_db ("quiz", $ link );
Mysql_query ("set names gb2312 ");
$ SQL = mysql_query ("select * from questions ");
Echo "<? Xml version = '1. 0' encoding = 'gb2312 '?> <Questions> ";
 
While ($ info = mysql_fetch_array ($ SQL )){
Echo "<ask>". $ info [question]. "</ask> <key>". $ info [answer]. "</key> ";
};
Echo "</questions> ";
?>

Questions. xml

<? Xml version = "1.0" encoding = "gb2312"?>
<Questions>
<Ask> which of the following is more efficient than the traditional web development mode and Ajax development mode? </Ask>
<Key> Ajax </key>
<Ask> What are the core objects of Ajax technology? </Ask>
<Key> XMLHttpResponse </key>
<Ask> what methods does Ajax use to locate the elements you need to operate on? </Ask>
<Key> getElementById </key>
<Ask> what technology does Ajax use to define display? </Ask>
<Key> CSS </key>
<Ask> which attribute of XMLHttpResponse stores the server's browser response (as an xml document )? </Ask>
<Key> responseXML </key>
</Questions>

That's easy. Let's take a look at this.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.