dongyue3795 2017-02-16 13:47
浏览 137

我正在使用两个jquery日期选择器来获取一周的开始和结束日期。 是否可以在7个单独的文本框中显示日期?

I'm a beginner and I would appreciate your help. I'm using two jquery datepickers to get the start and end date for a week. and this is my code for that:

Html and Php:

        <form method="post" action="">
            <div id="rangeDate">
                <input placeholder="Start Date" name="sDate" type="text" class="dateInput" id="startDate">
                <input placeholder="End Date" name="eDate" type="text" class="dateInput" id="endDate">
                <input type="submit" value="Submit"  name="submitBtn"><br>
            </div>
        </form>




   <?php

        include("C:/wamp/www/sunstar/DB_Connection.php");

        if(isset($_POST['submitBtn']))
        {
            $date1 = $_POST['sDate'];
            $date2 = $_POST['eDate'];

             if($date1 == ""){
                echo "<script>alert('Enter The Start Date!')</script>";
                    exit();
            }else{

            function returnDates($fromdate, $todate){

                $fromdate = \DateTime::createFromFormat('m/d/Y', $fromdate);
                $todate = \DateTime::createFromFormat('m/d/Y', $todate);
                return new \DatePeriod(
                    $fromdate,
                    new \DateInterval('P1D'),
                    $todate->modify('+1 day')
                );
            }

                $datePeriod = returnDates($date1, $date2);

                foreach($datePeriod as $date) {

                    echo "<form method='post' action=''>";
                    echo "<input id='specdates' name='7daysdates' type='text' value='" . $date->format('m/d/Y'), PHP_EOL . "' disabled><br>";
                    echo "</form>";

                }


                    $insert_date = "INSERT INTO tbldtr (DateFrom, DateTo) VALUES ('$date1', '$date2')";

                    if(mysqli_query($dbcon, $insert_date)){
                    echo "<script>alert('You have successfully inserted the date!')</script>";

                    }

             }/*END of Else Condition*/
         }

    ?>


   <script>

        var dateToday = new Date(); 
            $(function(){
                $( "#startDate" ).datepicker({
                    numberOfMonths: 1,
                    showButtonPanel: true,
                    maxDate: dateToday,
                });
                $('#endDate').datepicker({maxDate: dateToday});


                $('#startDate').change(function() {
                    var date2 = $('#startDate').datepicker('getDate', '+1d');
                    date2.setDate(date2.getDate()+6);
                    $('#endDate').datepicker('setDate', date2);
                });

            });


    </script>

The foreach($dateperiod as $date) loop could display the days in textboxes but I can't save them separately into the database because I only have 1 input text and it loops 7 times. What I want to achieve is to display them into 7 separate input text so I could save them into the database as FirstDay, SecondDay, ThirdDay, FourthDay, FifthDay, SixthDay and SeventhDay.

My purpose of saving them separately is because when I retrieve them the output should look like this:

This would be the output when I retrieved them into the database:

  • 写回答

1条回答 默认 最新

  • doumicheng6732 2017-02-16 13:56
    关注

    You can try this:

     echo "<input id='specdates' name='7daysdates[]' type='text' value='" . $date->format('m/d/Y'), PHP_EOL . "' disabled><br>";
    

    And then you can get each input field value by calling:

    $_POST['7daysdates'][0] => day one value
    $_POST['7daysdates'][1] => day two value
    $_POST['7daysdates'][2] => day three value
    $_POST['7daysdates'][3] => day four value
    $_POST['7daysdates'][4] => day five value
    $_POST['7daysdates'][5] => day six value
    $_POST['7daysdates'][6] => day seven value
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配