I'm doing a simple PHP quiz app which uses jquery, the rules are:
- each quiz has many questions (max 100), user clicks to add more questions, which generates a new form, appended at the end of the list
- many answers for each question (max 5), user clicks to add more answers for a question, appended at the end of the answer list of that question
- questions are sorted/weighted by submit sequence, ajax post in this case
- answers are named "answer[]"
- The quiz loaded from db, user can remove, edit or add new questions/answers within above limits
I've decided to organized each question a form with "class='postable'". My script for quiz updating looks like this
$("#update-change").click(function(e){
e.preventDefault();
showupdate('Updating..');
$('form.postable').each(function(){
$.ajax({
type: "POST",
url: 'update_ask.php',
data: $(this).serialize()
}).done(function( msg ) {
feedbackMessage(msg);
});
});
});
So, each time "#update-change" hit, there are n ajax posted to 'update_ask.php' to save quiz contents. The problem is the questions won't be sorted as seeing, it could be 'update_ask.php' fail to process ajax request in sequence due to lagging.
Do you have any idea/solution for this case? Please advise. Thanks.
HTML for one question sets looks like this:
<form action="update_ask.html" method="post" accept-charset="utf-8" name="aj_edit_ask2430" id="editaskform2430" class="postable">
<input type="hidden" name="idask" value="2430" />
<input type="hidden" name="idquiz" value="240" />
<div class="ask-holder">
<textarea name="ask" class="ask" tabindex="24300" placeholder="Question">cau 5</textarea>
<div class="right answer-image-holder"><img src="no-image.jpg" id="askimg2430" class="answerimg" alt="image" />
<input type="hidden" name="askimg" id="saskimg2430" value="" />
</div>
</div>
<div id="solution2430" class="answer-holder">
<div id="answer30684" class="answer-row">
<textarea name="answer[]" class="answer" tabindex="24301" placeholder="Answer or Solution">answer 51</textarea>
<div class="right answer-image-holder"> <img src="no-image.jpg" id="img30684" class="answerimg" alt="image" />
<input type="hidden" name="images[]" id="simg30684" value="" />
</div>
<span>
<input type="radio" name="iscorrect" value="1" checked='checked' />
Correct <a href="#row2430" onclick="removeAnswer('30684')" title="Remove this solution" class="removeanswer">Delete</a></span>
</div>
<div id="answer30685" class="answer-row">
<textarea name="answer[]" class="answer" tabindex="24302" placeholder="Answer or Solution">answer 52</textarea>
<div class="right answer-image-holder"> <img src="no-image.jpg" id="img30685" class="answerimg" alt="image" />
<input type="hidden" name="images[]" id="simg30685" value="" />
</div>
<span>
<input type="radio" name="iscorrect" value="2" />
Correct <a href="#row2430" onclick="removeAnswer('30685')" title="Remove this solution" class="removeanswer">Delete</a></span>
</div>
<div id="answer30686" class="answer-row">
<textarea name="answer[]" class="answer" tabindex="24303" placeholder="Answer or Solution">answer 53</textarea>
<div class="right answer-image-holder"> <img src="no-image.jpg" id="img30686" class="answerimg" alt="image" />
<input type="hidden" name="images[]" id="simg30686" value="" />
</div>
<span>
<input type="radio" name="iscorrect" value="3" />
Correct <a href="#row2430" onclick="removeAnswer('30686')" title="Remove this solution" class="removeanswer">Delete</a></span>
</div>
</div>
<a href="#row2430" onclick="return addSolution('2430')" title="Add one more solution">Add</a> <a href="#row2430" onclick="return removeAsk('2430','order2430')" title="Completely Delete this question">Delete this question</a>
</form>