Hi;

I've been searching around on the web for examples of how to code a very simple javascript-type date-picker into a php form page. I am absolutely new to use of javascript.

Most of the pre-coded examples I've found that I can get to work starts with a text/input box into which a user puts a date, clicks a button, and then sees a calendar page from which they can THEN pick the appropriate date.

I would like to find a control that opens with a simple, visually compact calendar page with the current date as the default/preselected value so that it is not a multi-click process to get to a recent date. (I assume the worst of my users).

So if anyone knows of such a beast, please point me in the right direction.

Thanks!

    Jquery and Datepicker would be my choice(and it has been many times 🙂 ). Its very customisable(but you dont have to if you like it as is). Jquery is also good to have as basis if you need anything else as it has dozens of good add-ons that you can use.

      Thank you. This looks very promising...I'll see if I can get it to work.

        Hi;

        The following code actually gets me to where I can see the calendar control (the visual formatting is kind of messed up, but that's not the issue at this point)

        BUT, I am not familiar with how to actually pass a value to the next point in the process (ie using the calendar as a selector in a form)

        The following should show the calendar, allow me to click a date and then hit the submit button, and the selected date should show at the top of the page when it reloads.

        <!DOCTYPE html>
        <html>
        <head>
          <link type="text/css" href="ui.all.css" rel="stylesheet" />
          <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
          <script type="text/javascript" src="ui.core.js"></script>
          <script type="text/javascript" src="ui.datepicker.js"></script>
          <script type="text/javascript">
          $(document).ready(function(){
            $("#datepicker").datepicker();
          });
          </script>
        </head>
        <body style="font-family: Arial; font-size: 6pt;>
        <?
        foreach($_POST as $key=>$value) {
        $$key=$value; 
        echo "$key: $value<br>"; // debug
        }
        ?>
        <form method="POST" action="jqtest1.php" target="_self">
        <table width="150"><tr><td>
        <!--- I don't think I'm using the calendar as an input control correctly here....--->
        <div type="text" id="datepicker"></div>
        <!--- ....I don't think I'm using the calendar as an input control correctly here.--->
        </td></tr></table>
        <input type="submit" value="test"> 
        </form> 
        </body>
        </html>

        Suggestions?

          This works, and passes the selected value as expected...but rather than displaying a text box with a calendar that appears on clicking, I just want the calendar to sit there and show whichever date the user clicked until the submit button is pressed.

          <!DOCTYPE html>
          <html>
          <head>
            <link type="text/css" href="ui.all.css" rel="stylesheet" />
            <script type="text/javascript" src="jquery-1.3.2.js"></script>
            <script type="text/javascript" src="ui.core.js"></script>
            <script type="text/javascript" src="ui.datepicker.js"></script>
            <script type="text/javascript">
            $(document).ready(function(){
              $("#datepicker").datepicker();
            });
            </script>
          </head>
          <body style="font-size:62.5%;">
           <?
          foreach($_POST as $key=>$value) {
          $$key=$value; 
          echo "$key: $value<br>"; // debug
          }
          $datedefault=date('m/d/Y');
          echo "<form method=\"POST\" action=\"jqtest2.php\" target=\"_self\"><div><input NAME=\"selecteddate\"  type=\"text\" value=\"$datedefault\" id=\"datepicker\"></div><input type=\"submit\" value=\"test\"></form>";
          ?>
          </body>
          </html>

            Sorry I actually have used v2 of the datepicker so I gave you the wrong link 🙁

            Check this out. It has many examples and also the one you are looking for.

            If I remember right you just add hidden field and set the value of that from "dateSelected" (theres the listener example in that sourcecode).

              Thank you! I will check it out.

              I have been messing around with Matt Kruse's Calendar Popup and kinda/sorta got something working. Maybe this code will help some other newbie in javascript calendars get started.

              Name this file "caltest.php" and put Matt Kruse's CalendarPopup.js in the same directory and it should work.

              <HTML>
              <HEAD>
              <TITLE>JavaScript Toolbox - Calendar Popup To Select Date</TITLE>
              </HEAD>
              <BODY BGCOLOR=#FFFFFF LINK="#00615F" VLINK="#00615F" ALINK="#00615F">
              <?php
              foreach($_POST as $key=>$value) { 
              $$key=$value; 
              echo "$key: $value<br>"; // debug 
              } 
              $datedefault=date('m/d/Y'); //put this in the date box to save users unneed step if data is current/today.
              ?>
              
              <!---GOT CalendarPopup.js FROM: http://www.mattkruse.com/javascript/calendarpopup/source.html--->
              
              <!---NEEDED FOR ALL VERSIONS--->
              <SCRIPT LANGUAGE="JavaScript" SRC="CalendarPopup.js"></SCRIPT>
              <SCRIPT LANGUAGE="JavaScript">document.write(getCalendarStyles());</SCRIPT>
              
              <!---VERSION FOR ANY DATE--->
              <SCRIPT LANGUAGE="JavaScript" ID="js1"> var cal1 = new CalendarPopup("testdiv1"); </SCRIPT>
              <SCRIPT LANGUAGE="JavaScript">writeSource("js1");</SCRIPT>
              
              <!---SELECT TODAY OR ANY FUTURE DATE--->
              <SCRIPT LANGUAGE="JavaScript" ID="js2"> var cal2 = new CalendarPopup("testdiv2"); 
              var thefuture = new Date();
              thefuture.setDate(thefuture.getDate() - 1);
              cal2.addDisabledDates(null, formatDate(thefuture,"yyyy-MM-dd"));
              </SCRIPT>
              <SCRIPT LANGUAGE="JavaScript">writeSource("js2");</SCRIPT>
              
              <!---SELECT TODAY OR ANY PAST DATE--->
              <SCRIPT LANGUAGE="JavaScript" ID="js3"> var cal3 = new CalendarPopup("testdiv3"); 
              var thepast = new Date();
              thepast.setDate(thepast.getDate() + 1);
              cal3.addDisabledDates(formatDate(thepast,"yyyy-MM-dd"), null);
              </SCRIPT>
              <SCRIPT LANGUAGE="JavaScript">writeSource("js3");</SCRIPT>
              
              <?
              echo "<table><tr><td><form method=\"POST\" action=\"caltest.php\" target=\"_self\">";
              echo "Something happened or will happen on: <INPUT TYPE=\"text\" NAME=\"date1\" VALUE=\"$datedefault\" SIZE=8> <A HREF=\"#\" onClick=\"cal1.select(document.forms[0].date1,'anchor1','MM/dd/yyyy'); return false;\" NAME=\"anchor1\" ID=\"anchor1\"  title=\"Click in box to select a different date.\">(select a date)</A><DIV ID=testdiv1></DIV>";
              echo "Something else WILL happen on: <INPUT TYPE=\"text\" NAME=\"date2\" VALUE=\"$datedefault\" SIZE=8> <A HREF=\"#\" onClick=\"cal2.select(document.forms[0].date2,'anchor2','MM/dd/yyyy'); return false;\" NAME=\"anchor2\" ID=\"anchor2\"  title=\"Click in box to select a different date.\">(select a future date)</A><DIV ID=testdiv2></DIV>";
              echo "Something else already HAPPENED on: <INPUT TYPE=\"text\" NAME=\"date3\" VALUE=\"$datedefault\" SIZE=8> <A HREF=\"#\" onClick=\"cal3.select(document.forms[0].date3,'anchor3','MM/dd/yyyy'); return false;\" NAME=\"anchor3\" ID=\"anchor3\"  title=\"Click in box to select a different date.\">(select a past date)</A><DIV ID=testdiv3></DIV>";
              echo "</td><td valign=bottom><input type=\"submit\" value=\"Submit Dates\"></td></FORM></tr></table>";
              ?>
              </BODY>
              </HTML>
              

                Okay...I think I've got all the basics set up correctly, but how do I now access their use in the context of a form?

                <HTML>
                <HEAD>
                <TITLE>jpquery calendar test</TITLE>
                <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
                <script type="text/javascript" src="date.js"></script>
                <script type="text/javascript" src="jquery.datePicker.js"></script>
                </HEAD>
                <BODY>
                <?php
                foreach($_POST as $key=>$value) { 
                $$key=$value; 
                echo "$key: $value<br>"; // debug 
                } 
                $datedefault=date('m/d/Y'); //put this in the date box to save users unneed step if data is current/today.
                ?>
                
                <!-- THIS STUFF WAS SHOWN ON THE INLINE CALENDAR EXAMPLE PAGE....  -->
                <!---http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/inlineDatePicker.html--->
                
                $(function()
                {
                	$('.turn-me-into-datepicker')
                		.datePicker({inline:true})
                		.bind(
                			'dateSelected',
                			function(e, selectedDate, $td)
                			{
                				console.log('You selected ' + selectedDate);
                			}
                		);
                });
                <!-- ...BUT NOT KNOWING JAVASCRIPT, I DON'T KNOW HOW TO APPLY IT TO A FORM  -->
                
                <?
                // a form that will eventually have a bunch of other inputs
                echo "<table><form method=\"POST\" action=\"luckcal1.php\" target=\"_self\">";
                echo "<tr><td>Something happened or will happen on: </td><td><INPUT TYPE=\"text\" NAME=\"date1\" VALUE=\"$datedefault\" SIZE=8></td></tr>";
                echo "<tr><td valign=bottom colspan=2><input type=\"submit\" value=\"Submit Date\"></td></FORM></tr></table>";
                ?>
                </BODY>
                </HTML>

                ....again, utterly new to Javascript...

                  I was not able to view the example source code in Internet Explorer, but I COULD see it in Firefox. This got me a little farther. Still not sure how to pass the selected date from the form though...

                  <HTML>
                  <HEAD>
                  <TITLE>jpquery calendar test</TITLE>
                  <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
                  <script type="text/javascript" src="date.js"></script>
                  <script type="text/javascript" src="jquery.datePicker.js"></script>
                  <!-- datePicker required styles -->
                  <link rel="stylesheet" type="text/css" media="screen" href="datePicker.css">
                  <!-- page specific styles -->
                  <link rel="stylesheet" type="text/css" media="screen" href="demo.css">
                  
                  <!-- page specific scripts -->
                  <script type="text/javascript" charset="utf-8">
                              $(function()
                              {
                  				$('.turn-me-into-datepicker')
                  					.datePicker({inline:true})
                  					.bind(
                  						'dateSelected',
                  						function(e, selectedDate, $td)
                  						{
                  							console.log('You selected ' + selectedDate);
                  						}
                  					);
                              });
                  </script>
                  
                  <style type="text/css">
                  		.turn-me-into-datepicker {
                  			float: left;
                  			margin: 10px;
                  		}
                  </style>
                  
                  </HEAD>
                  <BODY>
                  <?php
                  foreach($_POST as $key=>$value) { 
                  $$key=$value; 
                  echo "$key: $value<br>"; // debug 
                  } 
                  $datedefault=date('m/d/Y'); //put this in the date box to save users unneed step if data is current/today.
                  $td=date('m/d/Y'); //put this in the date box to save users unneed step if data is current/today.
                  
                  // a form that will eventually have a bunch of other inputs
                  echo "<table><form method=\"POST\" action=\"luckcal1.php\" target=\"_self\">";
                  echo "<tr><td>Something happened or will happen on: </td><td>"; ?>
                  			<div class="turn-me-into-datepicker">
                  				The contents of this div will be replaced by the inline datePicker.
                  			</div>
                  <? echo "</td></tr>";
                  echo "<tr><td valign=bottom colspan=2><input type=\"submit\" value=\"Submit Date\"></td></FORM></tr></table>";
                  ?>
                  </BODY>
                  </HTML>
                    <HTML>
                    <HEAD>
                    <TITLE>jpquery calendar test</TITLE>
                    <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
                    <script type="text/javascript" src="date.js"></script>
                    <script type="text/javascript" src="jquery.datePicker.js"></script>
                    <!-- datePicker required styles -->
                    <link rel="stylesheet" type="text/css" media="screen" href="datePicker.css">
                    <!-- page specific styles -->
                    <link rel="stylesheet" type="text/css" media="screen" href="demo.css">
                    
                    <!-- page specific scripts -->
                            <!-- page specific scripts -->
                    		<script type="text/javascript" charset="utf-8">
                                $(function()
                                {
                    				$('#date-view1')
                    					.datePicker({inline:true})
                    					.bind(
                    						'dateSelected',
                    						function(e, selectedDate, $td)
                    						{
                    							$('#date1').val(selectedDate.asString("yyyy-mm-dd"));
                    						}
                    					);
                                });
                    		</script>
                    
                    
                    <style type="text/css">
                    		.turn-me-into-datepicker {
                    			float: left;
                    			margin: 10px;
                    		}
                    </style>
                    
                    </HEAD>
                    <BODY>
                    <?php
                    foreach($_POST as $key=>$value) { 
                    $$key=$value; 
                    echo "$key: $value<br>"; // debug 
                    } 
                    $datedefault=date('m/d/Y'); //put this in the date box to save users unneed step if data is current/today.
                    $td=date('m/d/Y'); //put this in the date box to save users unneed step if data is current/today.
                    
                    // a form that will eventually have a bunch of other inputs
                    echo "<table><form method=\"POST\" action=\"luckcal2.php\" target=\"_self\">";
                    echo "<tr><td>Something is scheduled to happen on: </td><td>
                    <div class=\"turn-me-into-datepicker\" id=\"date-view1\">
                    The contents of this div will be replaced by the inline datePicker.
                    </div>
                    <input type=\"hidden\" name=\"date1\" id=\"date1\" />
                    </td></tr>";
                    echo "<tr><td valign=bottom colspan=2><input type=\"submit\" value=\"Submit Date\"></td></FORM></tr></table>";
                    ?>
                    </BODY>
                    </HTML>
                    

                    Now I just have to figure out the settings to allow user to click (only) past dates and both past and future dates. This current setup only shows dates from today onward.

                      You can set the daterange with startDate and endDate. Something like this(past dates):

                      $(function()
                      {
                      	$('#date-view1')
                      		.datePicker({
                      			inline:true,
                      			startDate: '01/01/1970',
                      			endDate: (new Date()).asString()
                      		})
                      		.bind(
                      			'dateSelected',
                      			function(e, selectedDate, $td)
                      			{
                      				$('#date1').val(selectedDate.asString("yyyy-mm-dd"));
                      			}
                      		);
                      });
                      

                      I must say I'm not a javascript guru and didnt test that one but it should work.

                      And ofcourse when you say both past and future dates you mean the default as every date is selectable 🙂

                        14 days later

                        I am closing this thread because I'm taking a somewhat different tack...the stuff I started with here was just too confusing for me as someone with no real Javascript knowledge.

                        See the new thread entitled:

                        "A SIMPLE javascript date picker! HOORAY! But..."

                          Write a Reply...