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 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算