2

I want the correct question number to be displayed in the database table but problem is that it is displaying incorrect question numbers by +1.

Lets say I have 4 rows and I used file input in row 2 and row 4 to upload the files, then it means the question numbers should be 2 and 4 meaning the table should look like this below:

ImageId    SessionId  QuestionId
01               AAA        2
02               AAA        4

But instead it has got the question number wrong by only plus 1 meaning it is displaying it like this:

ImageId    SessionId  QuestionId
01               AAA        3
02               AAA        5 

I believe the problem is that I am not inncrementing the question numbers correctly. Can anybody see what I am doing incorrectly in my code below that it is inserting the wrong question numbers?

Below is the code:

var qnum = 1; //question number starts at 1
var qremain = <?php echo (int)$_SESSION['textQuestion']; ?>; //the full number of questions

var $qid = $("<td class='qid'></td>" ).text(qnum);
var $image = $("<td class='image'></td>");    

var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='return imageClickHandler(this);' class='imageuploadform' >" + 
        "Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><br/><label class='imagelbl'>" + 
    "<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>" + 
    "<label><input type='button' name='imageClear' class='imageClear' value='Clear File'/></label>" +
    "</p></form>"); 


$tr.append($qid);
$tr.append($image);

$('.num_questions').each( function() {

    var $this = $(this);

    var $questionNumber = $("<input type='hidden' class='num_questionsRow'>").attr('name',$this.attr('name')+"[]")
                   .attr('value',$this.val());

    $qid.append($questionNumber);                             

    ++qnum;
    $(".questionNum").text(qnum);
    $(".num_questions").val(qnum);

    --qremain;
    $(".questionRemain").text(qremain);
});

How the $('.num_questions').each( function() { work is like this:

  • When user opens page, qnum (question number) starts with '1' at the top
  • When user appends a table row, then it will take question number '1' from the top and display it within the added row and the qnum (question number) at the top increments so that it now states 2
  • When user appends another row, it will take question number '2' from the top and display it within the added row and the qnum (question number) at the top increments so it now displays 3.
  • And so and so on...
4

1 に答える 1

0

Why not start with var qnum = 0;? Most incremental functions do!

EDIT

You're incrementing ++qnum before using .text and .val to display the value in the row. Try incrementing afterwards. Your each function would end with:

$(".questionNum").text(qnum);
$(".num_questions").val(qnum);
++qnum;

$(".questionRemain").text(qremain);
--qremain;
于 2012-09-20T16:02:48.237 に答える