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

时间:2017-10-14 15:53:32

标签: php html

我已经搜遍过,我找不到一个好的答案。 我有3个下拉菜单,用户可以在其中选择日期(所有3个都是从DB填充的)。我需要3个因为用户可以同时申请所有3个。现在我需要做的是至少需要一个。关于那个的任何想法?此外,名称是不同的,因为我发送到PHP函数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 个答案:

答案 0 :(得分:1)

客户端

为您的表单使用一些Javascript;只需检查以确保所有三个都等于空。如果是,则返回false;这将阻止提交表单,如果用户选择单个字段,则条件不再是true,并且可以提交表单。

注意:我正在使用jquery;但这很容易转录为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>

服务器端

您可以而且也应该在服务器端验证您的表单;这将阻止用户绕过你设置的任何参数 - 某个现存的。

我将继续使用相同的概念,具有if条件,只有当用户选择了至少一个值时,才会返回允许代码运行的条件。如果所有三个都是空的,那么它将回显错误并停止代码运行。

现在如果条件是假的;那么这意味着已经选择了一个或多个select值,并且用户被清除以继续......

// 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
    }
}