duangou6446 2017-10-14 15:53
浏览 48
已采纳

制作所需的精选下拉菜单之一

i have searched all over and i cannot find a good answer for this. I have 3 dropdowns where user can select a date (all 3 are populated from DB). I need 3 becouse user can apply to all 3 at the same time. Now what i need to do is make at lease one required. Any ideas about that? Also the names are different since i am sending this to php function mail() to send all the data to my mail.

      <select   name="prijava_okp" id="prijava_okp" >
            <option value="" selected="selected">Online klinični primeri </option>;
            <?php 
            include("config.php");
            session_start();
            if (!$db) {
                die('Could not connect: ' . mysqli_error($db));
            }
            $sql = mysqli_query($db, "SELECT ID_TECAJA, DATUM, HOUR FROM razpisani_tecaji WHERE STATUS ='odprt' AND ST_ODPRTIH_MEST>0 AND VRSTA=1");
            while ($row = $sql->fetch_assoc()){
                $row['DATUM'] = new DateTime($row['DATUM']);
                $dateFormated =  $row['DATUM']->format('d.m.Y');

                echo "<option value='" . $row['ID_TECAJA'] . "'>" . $dateFormated," ","ob"," ", $row['HOUR'] . "</option>";

            }
            ?>
        </select>
        <select   name="prijava_ops" id="prijava_ops" >
            <option value="" selected="selected">Online priprave na strokovni izpit</option>;
            <?php 
            include("config.php");
            session_start();
            if (!$db) {
                die('Could not connect: ' . mysqli_error($db));
            }
            $sql = mysqli_query($db, "SELECT ID_TECAJA, DATUM, HOUR FROM razpisani_tecaji WHERE STATUS ='odprt' AND ST_ODPRTIH_MEST>0 AND VRSTA=2");
            while ($row = $sql->fetch_assoc()){
                $row['DATUM'] = new DateTime($row['DATUM']);
                $dateFormated =  $row['DATUM']->format('d.m.Y');

                echo "<option value='" . $row['ID_TECAJA'] . "'>" . $dateFormated," ","ob"," ", $row['HOUR'] . "</option>";

            }
            ?>
        </select>
        <select   name="prijava_ods" id="prijava_ods" >
            <option value="" selected="selected">Online delavnice za študente medicine</option>;
            <?php 
            include("config.php");
            session_start();
            if (!$db) {
                die('Could not connect: ' . mysqli_error($db));
            }
            $sql = mysqli_query($db, "SELECT ID_TECAJA, DATUM, HOUR FROM razpisani_tecaji WHERE STATUS ='odprt' AND ST_ODPRTIH_MEST>0 AND VRSTA=3");
            while ($row = $sql->fetch_assoc()){
                $row['DATUM'] = new DateTime($row['DATUM']);
                $dateFormated =  $row['DATUM']->format('d.m.Y');

                echo "<option value='" . $row['ID_TECAJA'] . "'>" . $dateFormated," ","ob"," ", $row['HOUR'] . "</option>";

            }
            ?>
        </select><br><br>
  • 写回答

1条回答 默认 最新

  • doubaran2438 2017-10-14 16:09
    关注

    CLIENT-SIDE

    Use some Javascript for your form; just check to make sure that all three are equals to empty. If so, then return false; this will prevent the form from being submitted, if the user, then, selects a single field the condition is no longer true and the form can be submitted.

    NOTE: I am using jquery; but this can easily be transcribed to Javascript Vanilla.

    $('#submit').on('click', function() {
    
      // create variables
      var select1 = $('#select1');
      var select2 = $('#select2');
      var select3 = $('#select3');
      
      // validate form
      if ( select1.val() == '' && select2.val() == '' && select3.val() == '') {
        
        alert('You need to select at least one value!');
        return false; // do not submit
        
      }
      else
      {
        return true; // submit form
      }
    
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form id="dropdown" action="" method="post">
    
      <select name="select1" id="select1">
        <option value="">Select One</option>
        <option value="value2">Value2</option>
        <option value="value3">Value3</option>
        <option value="value4">Value4</option>
      </select>
      <select name="select2" id="select2">
        <option value="">Select One</option>
        <option value="value2">Value2</option>
        <option value="value3">Value3</option>
        <option value="value4">Value4</option>
      </select>
      <select name="select3" id="select3">
        <option value="">Select One</option>
        <option value="value2">Value2</option>
        <option value="value3">Value3</option>
        <option value="value4">Value4</option>
      </select>
    
      <button type="submit" name="submit" id="submit">Submit</button>
    
    </form>

    SERVER-SIDE

    You can, and should, also validate your form on your server side; this will prevent the user from bypassing whatever parameters you have set - to a certain extant.

    I will continue with the same concept, have an if condition which will only return allow the code to run only if the user has selected at least one value. If all three are empty then it will echo an error and will stop the code from running.

    Now if the condition is false; then that means that 1 or more select values have been selected and the user is cleared to continue...

    // first check for $_POST
    if ( isset( $_POST ) )
    {
        // create variables
        // filter them for any html tags
        $prijava_okp = htmlentities( $_POST['prijava_okp'] );
        $prijava_ops = htmlentities( $_POST['prijava_ops'] );
        $prijava_ods = htmlentities( $_POST['prijava_ods'] );
    
        // now check to see if all three are empty
        if ( empty( $prijava_okp ) && empty( $prijava_ops ) && empty( $prijava_ods ) )
        {
            // if they are all three empty `exit` the script
            // or use `header` to go back to the form
            echo 'You have to select at least one or more fields!';
            exit;
        }
        else
        {
            // ... run your code here
        }
    }
    
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题