One possibility would be, using jQuery, to store in an array the id of each question and its answers, and then compare them with the correct answers in the database.
I'll show you an example with only one question, but if you capture the idea, you can extend it to several questions by having a jQuery button at the end of the exam to get a tour getting the ids of each question and their answers, you would send that form serialized by POST to a PHP file that would send a single query to the bd that compares the answers given with the correct answers and returns the exam rating.
You can do it perfectly with a single click on the exam form, with a SQL query that would also return the result , of course you must capture the data in an array, send them to the DB and that the design of the same has a validation system of the correct answers.
It's just an idea, thought like that, fast. Maybe someone proposes something better.
$(function() {
$('#r1').click(function() {
var respuesta = [];
$(':checkbox:checked').each(function(i) {
respuesta[i] = $(this).val();
});
console.log(respuesta);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Examen</h1>
<p><b>1. ¿Quiénes han sido presidentes de los EE UU?</b></p>
<input name="r1_selector[]" id="r11_check" class="r_Checkbox" type="checkbox" value="1" />Barack Obama <br />
<input name="r1_selector[]" id="r12_check" class="r_Checkbox" type="checkbox" value="2" />Michael Jackson <br />
<input name="r1_selector[]" id="r13_check" class="r_Checkbox" type="checkbox" value="3" />Ronald Reagan <br />
<input name="r1_selector[]" id="r14_check" class="r_Checkbox" type="checkbox" value="4" />Fidel Castro <br />
<input type="button" id="r1" name="r1_n" value="Validar Respuesta 1" />
Note:
Thinking with a little more calm, you could even have in your database a table called examenes
in which you have a series of tests with the correct answers. These exams would be identified by a id
.
You would send by post to another PHP file an array with all the answers collected in the examen
, suppose that this exam has the id=1
, then you ask the BD for an array that you would have already defined with the correct answers of each Ask for that particular test and you make a comparison at the program level between the correct answers and the questions, accumulating the score.
Is that the question is a little wide. For example, the scoring system takes its work and its logic, because if in a question of multiple selection in which there are two possibilities of good response the student answers only one correctly, the score (imagine), would not be the same as if respond well to the two questions or if you answer all wrong.
To complete what would be a possible project, in order:
(a). PHP / HTML file with the exam in a form
<h1>Examen</h1>
<form id="examen" action="examinar.php" method="post">
<p><b>1. ¿Quiénes han sido presidentes de los EE UU?</b></p>
<input name="r1_selector[]" id="r11_check" class="r_Checkbox" type="checkbox" value="1" />Barack Obama <br />
<input name="r1_selector[]" id="r12_check" class="r_Checkbox" type="checkbox" value="2" />Michael Jackson <br />
<input name="r1_selector[]" id="r13_check" class="r_Checkbox" type="checkbox" value="3" />Ronald Reagan <br />
<input name="r1_selector[]" id="r14_check" class="r_Checkbox" type="checkbox" value="4" />Fidel Castro <br />
... más preguntas
<input type="button" id="btn_validar" name="r1_n" value="Validar Examen" />
</form>
<div id="resultado">Aquí se mostrará el resultado final del examen</div>
(b). jQuery that is thrown when pressing the button ** Validate ** of the form
This would be an example of an Ajax / jQuery function to post data from a form:
//Aquí se escucha el click del botón validar,
// se recogen los datos y se pasan a la función 'post_data()'
$( "#btn_validar" ).click(function(e)
{
var data = $(this).closest('form').serializeArray();
//Se pueden agregar otros valores al arreglo si se quiere
data.push({ name: this.name, value: this.value, name: "examinar", value:"examinar" });
e.preventDefault();
post_data(data) ;
});
//Esta función recibe el arreglo y hace el post
function post_data(data)
{
var frm=$( "#examen" );
var request = $.ajax
({
url: frm.attr("action"), //archivo php indicado en el atributo action del form
method: frm.attr('method'), //tipo de método indicado en el form
data: data, //datos obtenidos del form
dataType: "html"
});
request.done(function( msg )
{
$( "#resultado" ).html( msg );
return msg;
console.log(msg);
//alert (msg);
});
request.fail(function( jqXHR, textStatus )
{
alert( "Request failed: " + textStatus );
});
}
c. PHP file that processes the exam responses in the background
if(isset($_POST['examinar']))
{
//LLamada a la BD pidiendo las respuestas del examen 1
//Comparar respuestas
//Enviar nota final como respuesta
}