简体   繁体   English

php形式的Datepicker是必需的,带有隐藏的输入字段

[英]Datepicker is required in php form with hidden input field

I'm creating a php form to post data. 我正在创建一个php表单来发布数据。 One of the elements is a Datepicker JS element. 元素之一是Datepicker JS元素。 I pass the input from the datepicker to a hidden input field. 我将输入从日期选择器传递到隐藏的输入字段。 Now I want this datepicker to be required. 现在,我需要此日期选择器。 If I make the input field required it doesn't seems to work because it's hidden, and it must stay hidden. 如果我将输入字段设为必填字段,则该字段似乎无效,因为它是隐藏的,必须保持隐藏状态。 Anyone know the best way to do this? 有人知道最好的方法吗?

The code: 编码:

<div class="col-md-4 col-sm-12 ">
    <div id="date"></div>
    <label>Datum geselecteerd: </label>
    <input type="hidden" id="datefield" name="date" required/>
    <script type="text/javascript">
        var selected;
        $('#date').datepicker({beforeShowDay: function(date){
            return [date.getDay() != 1 & date.getDay() != 2 & date.getDay() != 3 & date.getDay() != 5 & date.getDay() != 0, ''];
        }});
        $(function() {
            $('#date').datepicker({ dateFormat: "dd-mm-yy" });
            $('#date').on("change",function(){
                selected = $(this).val();
                document.getElementById('datefield').value = selected;
            });
        });
    </script>
</div>

you can use .submit() in jquery which is called when form is submitted and you can check if date field is filled. 您可以在提交表单时调用的jquery中使用.submit(),并且可以检查日期字段是否已填写。 For more info here: https://api.jquery.com/submit/ 有关更多信息,请访问: https : //api.jquery.com/submit/

Also you should check it with php where you process the data the form sent you and if it is not filled you should send user back to fill the form again with a warning that he didnt fill a field 另外,您还应该使用php检查它,在其中处理发送给您的表单的数据,如果未填写,则应将用户送回以再次填写该表单,并警告他没有填写字段

example: 例:

<form id="myform" action="" method="get">
<input type="hidden" id="datefield" name="date" />
<input type="submit" />
</form>
<script>
$( "#myform" ).submit(function( event ) {
   if(  $("#datefield").val().length === 0 ) {
    alert('not filled');
    }
  event.preventDefault();
});
</script>

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

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