简体   繁体   中英

How to display correct number in alert

I am having issue with a question number displayed in an alert. Instead of displaying the question number, it is displaying the question id as the question number. So in the alert validation it states for example:

You have errors on question number: 115

When it should say You have errors on question number: 3 for example

Now this code display question id: value="<?php echo$questionId?>"

This code display question number: echo$searchQuestionNo[array_search($questionId, $searchQuestionId)]

But when I tried to do this:

<input type='hidden' id='num_groups' name='num_groups' value='<?php echo$searchQuestionNo[array_search($questionId, $searchQuestionId)]`?>'>

It does not display question number for jquery alert. To be honest what happens is that the validation doesn't work as it doesn't appear. Yet if I kept the above code to what it was which value was the question id:

<input type='hidden' id='num_groups' name='num_groups' value='<?php echo$questionId?>'>

Then it displays the questionId in the alert and the alert actually works.

So my question is how do I include the question number value in the alert yet be able to get the validation to work as well, like it does for questionid?

Below is main code so you can see where everything comes from:

PHP:

    <?

       // This will hold the search results
    $searchQuestionId = array();
    $searchQuestionNo = array();

    // Fetch the results into an array

   // get result and assign variables (prefix with db)
$stmt->bind_result($dbSessionId, $dbSessionName, $dbQuestionId, $dbQuestionNo, $dbQuestionContent, $dbAnswer, $dbAnswerId, $dbQuestionMarks, $dbOptionId, $dbOptionType);


        $searchQuestionId[] = $dbQuestionId;
        $searchQuestionNo[] = $dbQuestionNo;
      } 

?>



</head>

<body>


<form id="PenaltyMarks" action="<?php echo htmlentities($action); ?>" method="post">


<?php

$ques_ans = array();    //to store incorrect answers against ques no.

$q_occ_count = array_count_values($searchQuestionId);
foreach($searchQuestionId as $key => $questionId)
{
.....
}


?>
<table id='penaltytbl'>
<thead>
<tr>
<th class='questionth'>Question No.</th>
<th class='questionth'>Question</th>
</tr>
</thead>
<tbody>
<?php

foreach($ques_ans as $questionId => $inc_ans)
{


?>

<tr class="questiontd">

<td class="questionnumtd q<?php echo$searchQuestionNo[array_search($questionId, $searchQuestionId)]?>_qnum" rowspan="<?php echo$q_row_span?>"><?php echo$searchQuestionNo[array_search($questionId, $searchQuestionId)]?>
<input type="hidden" name="numQuestion" value="<?php echo$questionId?>" />
    </td>


<td class="answermarkstd">
<input class="individualMarks q<?php echo$questionId?>_mark"  q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="<?php echo$questionId?>" onkeypress="return isNumberKey(event)" maxlength="3" value="0" />
</td>

</tr>
<?php
}

?>
</tbody>
</table>

<p>
<input type='hidden' id='num_groups' name='num_groups' value='<?php echo$questionId?>'>
<input id="submitBtn" name="submitPenalty" type="submit" value="Submit Marks" />
</p>

</form>

Jquery:

  < script type = "text/javascript" >


    myClickHandler = function (e) {
      var ng = $('#num_groups').val();
      for (var group = 1; group 
           <= ng; group++) {
        if (!validation(group)) return false;
      }

    });


    function validation(group) {
      var msg = [];

      var nb = 0; // Number of blank values
      $("input[data-qnum='" + group + "']").each(function () {
        if ($(this).val() == '') {
          nb++;
          return false;
        }
      });
      if (nb != 0) {
        msg.push("\u2022 You have not entered in a value in all the Penalty Marks textbox \n");
      }

      if (msg.length >
          0) {
        alert("You have errors on Question Number: " + group + "\n\n" + msg.join("\n"));
        return false;
      } else {
        return true;
      }
    }




    < /script>

I assume that, as you said, this code permits to retieve the question number :

echo$searchQuestionNo[array_search($questionId, $searchQuestionId)]

Add your question number as an attribute q_number in your input element :

<input class="individualMarks q<?php echo$questionId?>_mark" q_number="<?php echo $searchQuestionNo[array_search($questionId, $searchQuestionId)]; ?>" q_group="1" name="answerMarks[]" type="text" data-type="qmark" data-qnum="<?php echo$questionId?>" onkeypress="return isNumberKey(event)" maxlength="3" value="0" />

And use it in your validation function

function validation(group) {
  var msg = [];

  var qNumber = null;

  var nb = 0; // Number of blank values
  $("input[data-qnum='" + group + "']").each(function () {

    //Assign the question number
    qNumber = $(this).attr('q_number');

    if ($(this).val() == '') {
      nb++;
      return false;
    }
  });
  if (nb != 0) {
    msg.push("\u2022 You have not entered in a value in all the Penalty Marks textbox \n");
  }

  //Use qNumber instead of the group variable
  if (msg.length > 0) {
    alert("You have errors on Question Number: " + qNumber + "\n\n" + msg.join("\n"));
    return false;
  } else {
    return true;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM