简体   繁体   English

出现错误信息时如何隐藏或不显示表单

[英]How to hide or not display form when error message appears

I have a live application here 在这里有一个现场申请

Please follow steps in order to use app: 请按照以下步骤使用应用:

  1. When you open the app, straight away click on the "Submit Course and Module" submit button. 打开应用程序后,立即单击“提交课程和模块”提交按钮。 You will see a javascript error message appear stating "Please Select a Course and Module". 您将看到一条JavaScript错误消息,提示“请选择课程和模块”。

  2. Now select Course "INFO101...." in the course drop down menu and Module "CHI2513...." in the module drop down menu and click on the submit button. 现在,在课程下拉菜单中选择课程“ INFO101 ....”,在模块下拉菜单中选择模块“ CHI2513 ....”,然后单击提交按钮。

  3. You will see a form display below where it contains text inputs appear which is fine. 您将在下面看到一个表单显示,其中包含文本输入,这很好。

  4. But this is where the problem lies. 但这就是问题所在。 Now if you look at the Course and Module drop down menus, they both state "Please Select". 现在,如果您查看“课程”和“模块”下拉菜单,它们都将显示为“请选择”。 So click on the "Submit Course and Module" submit button again. 因此,再次单击“提交课程和模块”提交按钮。 Now it displays the error message like it does in step 1, but it still displays the the form underneath (the text inputs). 现在,它像在第1步中一样显示错误消息,但仍显示下面的表单(文本输入)。 I don't want the form to be displayed underneath if the error message appears. 如果出现错误消息,我不希望该窗体显示在下面。

So my question is pretty much is that if the error message appears, is there a way I can hide the form or not display the form underneath. 所以我的问题是,如果出现错误消息,是否有办法隐藏表格或不显示下方的表格。 Which is ever is best practice? 最佳实践是哪一种?

Below is the code: 下面是代码:

Javascript code: JavaScript代码:

<script type="text/javascript">


     function validation() {

                var isDataValid = true;

                var courseTextO = document.getElementById("coursesDrop");
                var moduleTextO = document.getElementById("modulesDrop");

                var errModuleMsgO = document.getElementById("moduleAlert");

      if (courseTextO.value == "" && moduleTextO.value == ""){
          errModuleMsgO.innerHTML = "Please Select a Course and Module";
          isDataValid = false;
      } else if (courseTextO.value == ""){
          errModuleMsgO.innerHTML = "Please Select a Course";
          isDataValid = false;      
      }else  if (moduleTextO.value == ""){
          errModuleMsgO.innerHTML = "Please Select a Module";
          isDataValid = false;    
        }else{
                errModuleMsgO.innerHTML = ""; 
            }

            return isDataValid;

            }



    </script>

Below is the HTML/php: 以下是HTML / php:

<h1>EDIT AN ASSESSMENT'S DATE/START TIME</h1>   

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validation();">
<table>
<tr>
<th>Course: <?php echo $courseHTML; ?></th>
<th>Module: <?php echo $moduleHTML; ?></th>
</tr>
</table>
<p><input id="moduleSubmit" type="submit" value="Submit Course and Module" name="moduleSubmit" /></p>
<div id="moduleAlert"></div>
<div id="targetdiv"></div>
</form>

<?php

if (isset($_POST['moduleSubmit'])) {    

$outputmodule = ""; 

$moduleInfo = explode("_", $_POST['modules']);
$moduleId = $moduleInfo[0];
$moduleName = $moduleInfo[1];
$outputmodule = sprintf("<p><strong>Module:</strong> %s - %s</p>", $moduleId, $moduleName);


$editsession = "
<div id='rt-container'>
<form id='updateForm'>

    <p><strong>Current Assessment's Date/Start Time:</strong></p>
    <table>
    <tr>
    <th>Assessment:</th>
    <td><input type='text' id='currentAssessment' name='Assessmentcurrent' readonly='readonly' value='' /> </td>
    </tr>
    <tr>
    <th>Date:</th>
    <td><input type='text' id='currentDate' name='Datecurrent' readonly='readonly' value='' /> </td>
    </tr>
    <tr>
    <th>Start Time:</th>
    <td><input type='text' id='currentTime' name='Timecurrent' readonly='readonly' value=''/> </td>
    </tr>
    </table>
    <div id='currentAlert'></div>

    <p><strong>New Assessment's Date/Start Time:</strong></p>
    <table>
    <tr>
    <th>Assessment:</th>
    <td><input type='text' id='newAssessment' name='Assessmentnew' readonly='readonly' value='' /> </td>
    </tr>
    <tr>
    <th>Date:</th> 
    <td><input type='text' id='newDate' name='Datenew' readonly='readonly' value='' /> </td>
    </tr>
    <tr>
    <th>Start Time:</th> 
    <td><input type='text' id='newTime' name='Timenew' readonly='readonly' value=''/></td>
    </tr>
    </table>
    <div id='datetimeAlert'></div>

    </form>

    <p id='submitupdatebtn'><button id='updateSubmit'>Update Date/Start Time</button></p>

    </div>
";

echo $editsession;



}


?>

Since you're using jQuery, just do this: 由于您使用的是jQuery,只需执行以下操作:

  if (courseTextO.value == "" && moduleTextO.value == ""){
      $('#form').hide();
      errModuleMsgO.innerHTML = "Please Select a Course and Module";
      isDataValid = false;
  } 

By adding $('#form').hide(); 通过添加$('#form').hide(); to your condition, you ensure that anytime the message Please Select a Course and Module is displayed that the form will be hidden. 根据您的条件,请确保在任何时候显示消息“ Please Select a Course and Module都将隐藏该表格。

try something like 尝试类似

<?php if(!$error) { ?>
    <form id='updateForm'>
    .
    .
    .
     </form>

<?php } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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