How to Use jQuery + PHP + MySQL to implement an online test project, jquerymysql. How to Use jQuery + PHP + MySQL to implement an online test project. in the previous article of jquerymysql, we introduced the test results implemented by jQuery. This article will introduce how to use jQuery + PHP + MySQL to implement an online test project, jquerymysql
In the previous article, we introduced the effects of testing with jQuery. This article will introduce how to use jQuery + PHP + MySQL to implement online testing, including dynamically reading questions, scoring at the background after the answer, and returning the answer results. This is a comprehensive WEB application article. we recommend that you have basic knowledge about HTML, jQuery, PHP, MySQL, and so on.
Quiz. php
For convenience, I will mix php and HTML in the quiz. php file. First, load the jQuery library and the quizs. js file, and add the html structure of the test question to the appropriate location, just like the test answer function implemented by jQuery.
We need to read the question information when loading the page and display it to jQuery call. The question information comes from the database. you can first add the question and answer option information to the table quiz.
We construct an SQL statement, use PHP to query the database, and read the question and answer option information. Note that we do not need to read the correct answer at this time. Then, the question information is assigned to the variable $ JSON in json format.
<? Php shortde_once ("connect. php "); // connect to the database $ SQL =" select * from quiz order by id asc "; $ query = mysql_query ($ SQL ); // query data while ($ row = mysql_fetch_array ($ query) {$ answers = explode ('###', $ row ['answer']); // separate answer options $ arr [] = array ('question' => $ row ['id']. ','. $ row ['question'], // question 'answers' => $ answers // answer option);} $ json = json_encode ($ arr ); // Convert the json format?>
We get a string of data in json format and call jquizzy () as described in the previous article. the method is as follows:
$ (Function () {$ ('# quiz-INER'). jquizzy ({questions: <? Php echo $ json;?>, // Question message sendResultsURL: 'data. php' // result processing address });});
In this way, we can run the Web page quiz. php to check the source code. we can only see json data, but cannot see the answer part of the question.
Data. php
When calling the test question, there is an option sendResultsURL, which is to send data to the background when the user finishes the question and clicks the "finish" button. php sends an Ajax interaction request, data. php compares the correct answer based on the user's answer situation and then gives the user's score.
Include_once ("connect. php "); // connect to the database $ data =$ _ REQUEST ['any']; // Obtain the answer information $ answers = explode ('|', $ data ); // analyze data $ an_len = count ($ answers)-1; // number of questions $ SQL = "select correct from quiz order by id asc "; $ query = mysql_query ($ SQL); // query table $ I = 0; $ score = 0; // initial score $ q_right = 0; // number of correct questions while ($ row = mysql_fetch_array ($ query) {if ($ answers [$ I] = $ row ['correct']) {// compare the correct answer $ arr ['res'] [] = 1; // correct $ q_right + = 1; // number of correct answers + 1} else {$ arr ['res'] [] = 0; // error} $ I ++ ;} $ arr ['score '] = round ($ q_right/$ an_len) * 100); // calculate the total score echo json_encode ($ arr );
Data. in php, first connect to the database and receive the processing parameter an. an is the answer of the front-end user, then query the data table, and compare the answers submitted by the user with the correct answers of the questions in the data table, after comparison, perform corresponding processing, calculate the score obtained from the user's answer, and output the json format data to the front-end for calling.
Quizs. js
We modified the js code mainly for the ajax interaction section in the front and back. The core section of quizs. js is as follows:
If (config. sendResultsURL! = Null) {var collate = []; var myanswers = ''; // obtain the user's answer for (r = 0; r <userAnswers. length; r ++) {collate. push ('{"questionNumber": "' + parseInt (r + 1, 10) + '", "userAnswer ": "'+ userAnswers [r] +'"} '); myanswers = myanswers + userAnswers [r] +' | ';} // Ajax interaction $. getJSON (config. sendResultsURL, {an: myanswers}, function (json) {if (json = null) {alert ('communication failed! ');} Else {var corects = json ['res']; $. each (corects, function (index, array) {resultSet + =''+ (Corects [index] = 1? "
# "+ (Index + 1) +"
":"# "+ (Index + 1) +"
") +'';}); ResultSet = ''+ judgeSkills (json. score) +'
Your score: '+ json. score +''+ ResultSet +''; Supercontainer.find('.result-keeper'{.html (resultSet). show (500 );}});}
After a user answers a question, the answer is a string consisting of "1 | 2 | 4 | 1 | 3 |", and then passed $. getJSON submits the answer to the parameter an to the backend. after the backend PHP compares the correct answer, the comparison result is returned. the returned result is {"res, 0], "score": 60}, res is the answer comparison result, indicating the answer results of the five questions respectively, 1 indicates that the answer is normal, 0 indicates that the answer is incorrect, and score indicates the score. Then, the returned results are processed to obtain the evaluation results and total scores of each question, and generate the corresponding html structure.
MySQL
Finally, the structure of the mysql data table quiz is attached:
CREATE TABLE IF NOT EXISTS `quiz` ( `id` int(11) NOT NULL AUTO_INCREMENT, `question` varchar(100) NOT NULL, `answer` varchar(500) NOT NULL, `correct` tinyint(2) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
You can add information to the table or directly import the quiz. SQL file in the source package.
The above is all the content of this article. I hope you will like it.
In the previous article, we introduced the test results implemented by jQuery. Then this article will be combined with examples...