【问题标题】:Make datalist required需要数据列表
【发布时间】:2016-11-24 01:07:03
【问题描述】:

我希望用户不能提交表单,除非他们从下拉列表datalist 中选择了一些内容,该下拉列表在他们输入时出现。如果他们只是随机输入内容,我希望提交表单。

如果这不可能,更好的选择是在用户提交时检查输入的文本是否出现在数据列表中?

<form method="post">
    <input type="text" list="browsers">
    <datalist id="browsers">
        <option value="Internet Explorer">
        <option value="Firefox">
        <option value="Chrome">
        <option value="Opera">
        <option value="Safari">
    </datalist>
    <input type="submit" name="submit">
</form>

<?php 
if(isset($_POST['submit']
   // function to check if something from the datalist was clicked and NOT just typed
}else{
   echo'Select something from the datalist!';
}

虽然我可以根据需要设置datalist,但这很容易被绕过。

【问题讨论】:

    标签: php html html-datalist


    【解决方案1】:

    input 标记中使用required 与您的目标数据列表具有相同的 id 将检查强制用户输入内容。

    <form method="post">
        <input type="text" list="browsers" required>
        <datalist id="browsers">
            <option value="Internet Explorer">
            <option value="Firefox">
            <option value="Chrome">
            <option value="Opera">
            <option value="Safari">
        </datalist>
        <input type="submit" name="submit">
    </form>
    
    <?php 
    if(isset($_POST['submit']
       // function to check if something from the datalist was clicked and NOT just typed
    }else{
       echo'Select something from the datalist!';
    }
    

    但是,它不会阻止用户提供未在下拉列表中列出的输入。这需要在提交前通过 Javascript 进行检查。

    <form method="post" onsubmit="return myCheckFunction(this)>
    
      <!-- Your form items -->
      <!--------------------->
    
    </form>
    
    <script>
      myCheckFunction(form) {
        // get the values that are currently under the datalist tag in option tags
        // get the user input
        // compare the options with the user input
        // if one is equal with the user input submit the form with the method submit();
        // else don't submit the form, the user will have to change his input
      }
    </script>
    

    如果您希望对 js 有所帮助,我很乐意提供帮助。

    【讨论】:

    • 您还必须执行服务器端验证。很容易绕过客户端验证。
    • JavaScript 是唯一的选择吗?我真的希望不必使用它。
    • 我没有任何其他想法:使用 JavaScript 实际上很容易。在提交时,您运行一个函数来检查输入是否 == 选项值之一。如果是这样提交可以继续,否则提交不会发生。如果您愿意,我可以帮助您处理 js。
    • 不过,JavaScript 也是在客户端完成的
    • 选项是否是动态的没有任何区别,检查将在客户端提交之前进行。
    【解决方案2】:

    如果您想使用服务器端验证,

    <form method="post">
        <input type="text" list="browsers" name="items" required>
        <datalist id="browsers" >
            <option value="Internet Explorer">
            <option value="Firefox">
            <option value="Chrome">
            <option value="Opera">
            <option value="Safari">
        </datalist>
        <input type="submit" name="submit">
    </form>
    
    <?php 
    if(isset($_POST['submit']
       $check = array('Internet Explorer', 'Firefox', 'Chrome', 'Opera', 'Safari');
    
    if(in_array($_POST['items'], $check)){
      // Code to be execute. 
    } else{
       echo'Select something from the datalist!';
    }
    
    }else{
       echo'Select something from the datalist!';
    }
    

    【讨论】:

    • 如果datalist是动态的,数据从哪里来?如果数据来自 API,并且 API 调用来自 PHP,则您的动态 $check 数组已经可用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 2016-12-12
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多