How to use jQuery PHP MySQL to implement an online testing project
This article will use examples to introduce how to use jQuery PHP MySQL to implement online test questions, including dynamic reading of questions, background scoring after answering the questions, and returning the answer results.
In the previous article, we introduced the test question effect achieved using jQuery. Then this article will use examples to introduce how to use jQuery PHP MySQL to implement online test questions, including dynamic reading of questions, background scoring after answering the questions, and returning the answer results. This is a comprehensive WEB application article. It is recommended that you who read this article should have basic knowledge of HTML, jQuery, PHP and MySQL.
Quiz.php
For the convenience of explanation here, I mixed php and HTML in the quiz.php file. First, it is the same as the previous article on this site: the test answering function implemented by jQuery. Load the jQuery library and quizs.js file, and then add the test question html structure at the appropriate location.
?
|
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
include_once("connect.php");//连接数据库
$sql = "select * from quiz order by id asc"; $query = mysql_query($sql); //查询数据 while($row=mysql_fetch_array($query)){ $answers = explode('###',$row['answer']); //将答案选项分开 $arr[] = array( 'question' => $row['id'].'、'.$row['question'], //题目 'answers' => $answers //答案选项 ); } $json = json_encode($arr); //转换json格式 ?> |
We construct a 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 assign the question information to the variable $json in JSON format.
?
|
<🎜>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 json format ?> |
1 2 3 4 5 6 | $(function(){ $('#quiz-container').jquizzy({ questions: , //question information sendResultsURL: 'data.php' //Result processing address }); }); |
In this way, let’s run the web page quiz.php again to see if a test question is generated. Looking at the source code, we can only see the json data, but not the answer part corresponding to the test question.
data.php
When calling the test question, there is an option sendResultsURL. When the user finishes typing the question and clicks the "Finish" button, it sends an Ajax interactive request to the background data.php. data.php will respond based on the user's answer. , compare the correct answers, and then give the user's score.
?
|
include_once("connect.php"); //Connect to the database $data = $_REQUEST['an']; //Get 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 answers while($row=mysql_fetch_array($query)){ if($answers[$i]==$row['correct']){ //Compare the correct answers $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); |
In data.php, first connect to the database and receive the processing parameter an, which is the answer to the front-end user's answer. Then query the data table, compare the answer submitted by the user with the correct answer to the question in the data table, and make corresponding decisions after comparison. Processing, and calculates the score obtained by the user's answer, and finally outputs and returns json format data to the front desk call.
Quizs.js
We have modified the js code, mainly for the front and backend ajax interaction part. The core parts of quizs.js are as follows:
?
2 3 4 5
|
if (config.sendResultsURL !== null) {
var collate = [];
var myanswers = '';
//Get the answers to the questions answered by the user
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) " ") '< /div>';
});
resultSet = '' judgeSkills(json.score) '' resultSet '';
superContainer.find('.result-keeper').html(resultSet).show(500);
}
});
}
|
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 the entire content of this article, I hope you all like it.