php validation before submit 实例 && 一些需要规避的问题

来源:互联网 发布:快递打印软件破解 编辑:程序博客网 时间:2024/06/04 18:53

表单查询之前 ,需要检查条件

规避:

a. 

  submit 按钮一定要使用 input ,不可以使用button 不然不起作用

<input type="submit" style="width: 80%" onclick="return checkTime();" class="btn btn-primary  btn-block" value="查询">


b.

   检测函数的要点:

    检测的函数 如果不想被submit 的话  返回 false  ;如果想要被提交的话 返回true 

<!DOCTYPE html><html><head>    <include file="Common@Public/head" />    <script type="text/javascript" src="//cdn.jsdelivr.net/jquery/1/jquery.min.js"></script>    <script type="text/javascript" src="//cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>    <script src="__JS__select2.full.min.js"></script>    <link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap/3/css/bootstrap.css" />    <link rel="stylesheet" href="__CSS__select2.min.css"></head><body><include file="Common@Public/dhb_info" /><include file="Common@Public/header" /><div class="container">    <div id="breadcrumb_box">        <include file="Common@Public/nav" />    </div></div><div class="container"><div class="panel panel-default">    <div class="panel-body">        <form class="form-horizontal" action="{:U('')}" method="get">            <div class="form-group">                <label  class="col-sm-2 control-label">开始时间</label>                <div class="col-sm-5">                    <input type="date" name="begin" id="time_begin" class="form-control"  value="<?= (!isset($input['begin']) || !$input['begin']) ? '' : $input['begin']; ?>"/>                </div>            </div>            <div class="form-group">                <label  class="col-sm-2 control-label">结束时间</label>                <div class="col-sm-5">                    <input type="date" name="end" id="time_end" class="form-control"  value="<?= (!isset($input['end']) || !$input['end']) ? '' : $input['end']; ?>"/>                </div>            </div>            <div class="form-group">                <label class="col-sm-2 control-label">选择客户</label>                <div class="col-sm-5">                    <select class="form-control" id="choose_user_sel" name="id">                        <?php if (isset($input['developer'])) { ?>                            <option value="{$input['id']}" >{$input['developer']}</option>                        <?php } ?>                        <option value="" >选择客户</option>                        <foreach name="user_list"  item="vo">                            <option value="{$vo['id']}" >{$vo['developer']}</option>                        </foreach>                    </select>                </div>            </div>            <div class="form-group">                <div class="col-sm-offset-2 col-sm-10">                    <input type="submit" style="width: 80%" onclick="return checkTime();" class="btn btn-primary  btn-block" value="查询">                </div>            </div>        </form>    </div></div></div><div class="container">    <div class="panel panel-default">        <table class="table table-hover table-bordered">            <tr align="center">                <th >客户ID</th>                <th >客户名称</th>                <th >传入详单数</th>                <th >有效详单数</th>            </tr>            <tr>                <td> </td>                <td>总计 </td>                <td><?= isset($total_data['access_counts']) ? $total_data['access_counts'] : 0 ;?></td>                <td><?= isset($total_data['success_counts']) ? $total_data['success_counts'] : 0 ;?></td>            </tr>            <?php            foreach ($user_show as $stat) { ?>            <tr>                <td>{$stat['id']}</td>                <td>                    <a href="{:U('cuishouDetail',['id'=>$stat['id']])}">{$stat['developer']}</a>                </td>                <td><?= isset($stat['access_counts']) ? $stat['access_counts'] : 0 ;?></td>                <td><?= isset($stat['success_counts']) ? $stat['success_counts'] : 0;?></td>            </tr>            <?php } ?>        </table>    </div>    <if condition="$page">        <ul class="pagination">            {$page}        </ul>    </if></div></div><script type="text/javascript">    $(document).ready(function() {        $("#choose_user_sel").select2();    });    function checkTime() {        // get begin time and end time        var time_begin = $('#time_begin').val();        var time_end = $('#time_end').val();        var today_str = (new Date()).toDateString();        // set default time        if (!time_begin) {            time_begin = today_str;        }        if (!time_end) {            time_end = today_str;        }        // calculate the days between begin and end        var day_diff =  Math.floor(Math.abs((Date.parse(time_end) - Date.parse(time_begin)) /8.64e7));        //  the time should less than 31        if (day_diff <= 28) {            $('cuishouForm').submit();            return true;        } else {            alert('超出单次时间的查询范围,请将查询范围缩短到30天内');            return false;        }    }</script></body></html>

原创粉丝点击