How to Use jQuery + PHP + MySQL to implement an online test project_php tutorial

Source: Internet
Author: User
How to Use jQuery + PHP + MySQL to implement an online test project. How to Use jQuery + PHP + MySQL to implement an online test project how to use jQuery + PHP + MySQL to implement an online test project this article will introduce how to use jQuery + P with examples. jQuery + PHP + MySQL to implement an online test project

 How to Use jQuery + PHP + MySQL to implement an online test project

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.

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.

?

1

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.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Include_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 options

);

}

$ 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:

?

1

2

3

4

5

6

$ (Function (){

$ ('# Quiz-INER'). jquizzy ({

Questions: , // Question information

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.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

Include_once ("connect. php"); // connect to the database

$ Data = $ _ REQUEST ['any']; // Obtain 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 a table

$ I = 0;

$ Score = 0; // initial score

$ Q_right = 0; // number of correct answers

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); // calculates 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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

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:

?

1

2

3

4

5

6

7

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.

How to use jQuery + PHP + MySQL to implement an online test project this article will introduce how to use jQuery + P...

Related Article

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.