How to display echo if user has not selected from a drop down menu

前端 未结 2 333
面向向阳花
面向向阳花 2021-01-25 05:11

In my code below I have 2 drop down menus. One is a \"Course\" drop down menu and the other is a \"Modules\" drop down menu:

Below is the code:

   

        
相关标签:
2条回答
  • 2021-01-25 05:58

    You can do this using javascript create a function inside it where you can check weather each field is filled or not can be displayed there like this

    <script type="text/javascript">
    function validate()
    {
    if(document.dropdownmenuname.course.value=="")
    {
    alert("Please fill in your form");
    document.dropdownmenuname.course.focus() ;
     return false;
    }
    }
    ....
    
    0 讨论(0)
  • 2021-01-25 06:08

    Change your form tag

    <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    

    to

    <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validation();">
    

    It's a good idea to validate on serverside too, just add a little code in your existing PHP if you want to check values on serverside:

    <?php
    
    if (isset($_POST['moduleSubmit'])) {
    
    if(empty($_POST['courses']) && empty($_POST['modules'])) {
       echo 'Please Select a Course and Module';
    } elseif(empty($_POST['courses'])) {
       echo 'Please Select a Course';
    } elseif(empty($_POST['modules'])) {
       echo 'Please Select a module';
    } else {
    
    $sessionquery = "
    SELECT SessionId, SessionDate, SessionTime, ModuleId, TeacherId
    FROM Session
    WHERE
    (ModuleId = ? AND TeacherId = ?)
    ORDER BY SessionDate, SessionTime 
    ";
    
    $sessionqrystmt=$mysqli->prepare($sessionquery);
    // You only need to call bind_param once
    $sessionqrystmt->bind_param("si",$moduleId,$userid);
    // get result and assign variables (prefix with db)
    
    $sessionqrystmt->execute(); 
    
    $sessionqrystmt->bind_result($dbSessionId,$dbSessionDate,$dbSessionTime, $dbModuleId, $dbTeacherId);
    
    $sessionqrystmt->store_result();
    
    $sessionnum = $sessionqrystmt->num_rows();   
    
    if($sessionnum ==0){
    echo "<p>Sorry, You have No Sessions under this Module</p>";
    
    }
    
    0 讨论(0)
提交回复
热议问题