【问题标题】:How to validate a form and check if each question is answered?如何验证表格并检查每个问题是否已回答?
【发布时间】:2022-02-04 17:19:34
【问题描述】:

问题保存在数据库中。为了让用户看到问题,它和答案一样被循环播放。 (不知道我解释的对不对)

我将提供表单的整个代码,我真的不知道如何处理这个。我想要实现的是,如果问题没有得到解答,用户将无法单击下一步或提交。但是这段代码,即使用户没有回答,只是点击下一步并提交,它也会提交。所以最终用户可以得到 0 分,这不应该是这种情况。

    <form action="applicantAssessmentSave.php" method="post" id="register_form">

       <div class="col-lg-6 col-md-6 col-sm-12">
       <div class="form-group">
        <label for="finished_degree" style="color:#fff";>Finished Degree</label>
        <select class="form-control" name="finished_degree" id="finished_degree"   onchange= "handleSelectChange(event)" required>
         <option value="" selected disabled >Select your finished degree</option>
         <option value="BSIT">BSIT</option>
         <option value="BSHM">BSHM</option>
         <option value="BSBA">BSBA</option>
        </select>
      </div>
                                            
    <script type="text/javascript">
      function handleSelectChange(event){
         var selectElement = event.target;
         var value = selectElement.value;

            $.ajax({
                url:"degree_job_match.php", 
            type: "post",    
            data: {
                "value": $('#finished_degree').val(),
            }, 
            success:function(data){
            }
        });
     }
    </script>
    </div>
        <?php
            $m = 1;
            $query = mysqli_query($con, "select * from question_categories") or die(mysqli_error($con));
            $count = mysqli_num_rows($query);
            while ($row = mysqli_fetch_array($query)) {
            $id = $row['id'];
            $question = mysqli_query($con, "select * from question where cat_id='".$id."' ") or die(mysqli_error($con));   
                ?>  
        <fieldset>
           <input type="hidden" name="cat_id[]" value="<?php echo $row['id'] ?>">
             <div class="card" style="background-color: #fff; " > 
             <div class="card-body" style="margin-left: 20px;">
             <div class="form-check form-check-inline">
        <?php 
          while ($question_row = mysqli_fetch_array($question)) {
         ?>
           <input type="hidden" name="question_id[<?php echo $row['id'] ?>][<?php echo $question_row['id'] ?>]" value="<?php echo $question_row['id'] ?>">     
        <div class="form-group">
         <label style="font-weight: 900;">
           <strong>
            <?php echo $question_row['question']; ?>
           </strong>
         </label>
        <br>
        <div class="form-check">
         <input class="form-check-input" type="radio" name="answer[<?php echo $row['id'] ?>][<?php echo $question_row['id'] ?>]" value="5" required>
        <label class="form-check-label">
            Strongly Agree
        </label>
       </div>
    <br>
        <div class="form-check">
        <input class="form-check-input" type="radio" name="answer[<?php echo $row['id'] ?>][<?php echo $question_row['id'] ?>]" value="4" required>
    <label class="form-check-label">
        Agree
    </label>
</div>
    <br>
    

    <div class="form-check">
        <input class="form-check-input" type="radio" name="answer[<?php echo $row['id'] ?>][<?php echo $question_row['id'] ?>]" value="3" required>
        <label class="form-check-label">
        Neutral
        </label>
        </div>
       <br>
        <div class="form-check">
        <input class="form-check-input" type="radio" name="answer[<?php echo $row['id'] ?>][<?php echo $question_row['id'] ?>]" value="2" required>
        <label class="form-check-label">
            Disgree
        </label>
       </div>
         <br>
            <div class="form-check">
            <input class="form-check-input" type="radio" name="answer[<?php echo $row['id'] ?>][<?php echo $question_row['id'] ?>]" value="1" required>
            <label class="form-check-label">
              Strongly Disagree
            </label>
        </div>
        <br>
        </div>
       <?php } ?>
      </div>
      </div>
     </div>
              <?php if($m!=$count){ ?>
                <button name="" id="" class="btn btn-primary next-form btn-sm" type="button" style="background-color: #fff200; color:#000;">
                 Next
                 </button>
                <?php } if($m!=1){ ?>
                 <button name="" id="" class="btn btn-warning previous-form" type="button">
                  Previous
                 </button>
                <?php } ?>
                 <?php if($m==$count){ ?>
                    <button  class="btn btn-info submit" id="submit_form">
                       Submit
                    </button>
                    <?php } ?>
          </fieldset>
             <?php
                    $m++;
         }
         ?>
      </form>

更新!!!

我删除了按钮引导程序的类。现在它验证是否未选择该选项。现在我的问题是,即使第一组问题都答对了,点击按钮也无法跳转到下一页。

更新 2 我想这是因为这个脚本

  $(document).ready(function(){  
  var form_count = 1, previous_form, next_form, total_forms;
  total_forms = $("fieldset").length;  
  $(".next-form").click(function(){
        previous_form = $(this).parent();
        next_form = $(this).parent().next();
        next_form.show();
        previous_form.hide();
        setProgressBarValue(++form_count);
  });  

我现在该怎么办?

【问题讨论】:

  • 请编辑您的代码以修复缩进,使其更具可读性。你会使用 javastipt 吗?
  • type=button 的按钮不会触发浏览器内验证。对于您的“下一步”按钮,您需要使用 javascript。 required 属性应该足以用于实际的表单提交和浏览器验证。
  • 嗨!正如你所说,我尝试了javascript。但是还是有问题,是的,它检查单选按钮是否被选中,但它仍然进入下一页。使用此代码function atLeastOneRadio() { return($('input[type=radio]:checked').size()&gt;0); }

标签: javascript php html jquery


【解决方案1】:

(1) 如果你想让用户没有答案就不能提交,你应该在单选按钮上添加required。 (2)如果你想让用户在没有答案的情况下提交,我认为它运行得很好。 但是由于 Bootstrap,它应该是:

 <div class="form-check">
      <input class="form-check-input" type="radio" name="answer[<?php echo $row['id'] ?>][<?php echo $question_row['id'] ?>]" value="5">
      <label class="form-check-label">
        Strongly Agree
      </label>
    </div>
<div class="form-check">
      <input class="form-check-input" type="radio" name="answer[<?php echo $row['id'] ?>][<?php echo $question_row['id'] ?>]" value="4">
      <label class="form-check-label">
        Agree
      </label>
    </div>

如果您有任何其他信息,请告诉我。

【讨论】:

  • 在我的表单中,用户必须回答每个问题。它不能为空。我的代码发生的情况是,即使没有回答问题,它仍然可以提交。我尝试在单选按钮中添加 required 但它不起作用。提交前必须先填写表格。每个问题都应选中 1 个单选按钮。
  • 恐怕是因为您的单选按钮被包装在标签中。请尝试将它们分开。
  • 我现在尝试将它们分开,但是当点击下一个按钮时,即使没有答案它仍然提交
  • 我编辑了我的帖子,现在我认为这里的按钮可能是错误的。
  • 抱歉确认。每个问题都在一个表格上吗?以 form 提交时,Required 正在工作。包装在表单中时,该按钮应为 auto type=Submit。如果可以的话,你愿意分享给我们吗?你的完整表格?
猜你喜欢
  • 2021-01-24
  • 1970-01-01
  • 2015-10-03
  • 2019-04-03
  • 2016-06-20
  • 1970-01-01
  • 2015-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多