Reputation: 59
I have implemented an online quiz using jQuery. I have this specific requirement I want to implement at the end of the quiz.
if (number_of_answered_questions == total_questions) {
save the score to the database;
redirect to redirect_url;
}
This redirect_url
depends on the score. (for ex: if the score < 10
, redirect to page1.html and if 10 < score < 15
, redirect to page2.html)
I tried using jQuery.post
, but I'm not sure how to implement both in the same 'post'.
How can I implement these two tasks in jQuery?
I can't write the logic to "save to database" in the redirect pages because they keep changing. (I mean admin can change the redirect pages).
Upvotes: 0
Views: 656
Reputation: 18233
You can trigger a redirect in JS by modifiying window.location.href
. You simply need to change your pseudocode/conditionals into the JS equivalent, and then modify the href
accordingly to redirect, once your save completes successfully (i.e. in the callback for the post
method).
var total_questions = ...;
var number_of_answered_questions = ...;
var score = ...;
// other quiz logic...
var postData = { "score": score, ... };
if (number_of_answered_questions == total_questions) {
$.post("/saveScores.php", postData, function() {
if (score < 10) {
window.location.href = "page1.html"
}
else if (10 < score && score < 15) {
window.location.href = "page2.html";
}
});
}
Upvotes: 1