Hi,
I have a datepicker in a div to ensure that it is displayed permanently.
Stupid thing is I cannot get the date chosen. Somebody clicks on a date. How do I get that into my php programme?

Dave

Member Avatar for diafol

Is it within a form tag? Are you submitting the form? Are you using Ajax?
If you posted your code, we'd have an idea of what you were doing. Help us to help you - post your code.

OK

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

  <script src="//code.jquery.com/jquery-1.10.2.js"></script>

  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

  //<link rel="stylesheet" href="/resources/demos/style.css">
   <script>

  $(function() {

    $( "#datepicker" ).datepicker({ 
    minDate: -1, 
    changeMonth: true, 
    changeYear: true, 
    dateFormat: "yy/mm/dd"

     });

  });



  </script>

In my root directory/getdate.php I have:

  <div id="datepicker"></div>



<script>

$( "#datepicker" ).datepicker();
 var currentDate = $( ".selector" ).datepicker( "getDate" );
 document.write (currentDate);
</script>

Which does not work
Member Avatar for diafol

Bit confused. You want to send this data to the server (PHP). In order to send data to the server, you have a few choices, including a form with submission button. This takes a traditional submission (as declared in the method and action properties of the form tag). Alternatively use an Ajax routine - which transports the data (datepicker) via javascript to the server. You seem to have neither.

You are using jQuery already, so sending via $.ajax or $.post (or $.get if you're just requesting info as opposed to making changes on the server).

Yes, this data will be used in a query to get and display the results.
I can do what I want using a form with jquery embedded (whatever the term should be) which seems to mean, unfortunately, that the input field needs to be clicked on to open datepicker and the submit button of the form clicked on to complete.
If, on the other hand, I put the form on within a div then it is there when the page opens. I must have a look at Ajax if it can do the job datepicker > value > sql.
My concern at the moment is the user may look at the input box, without the date picker, and miss inputting data.
I am also curious to know why the date selected in my code is not written using the document.write.

regards

Dave

Member Avatar for diafol

Is there a visible date in the input immediately after page load? If not, then there is nothing to put into currentDate and therefore nothing to display. You haven't bound currentDate dynamically to the datepicker, so it won't be updated when you change the date (nor supply the date in the first place).

BTW use the console.log() to show output (in the console panel of your browser), it's much cleaner than document.write() and alert().

Also not sure what this .selector is all about. Can't see this in the DOM.

I think I will have to spend a bit more time learning javascript and php. I have just discovered what json, ajax, plunker and jsfiddle are about. I should be able to start using a proper debugger to at least see what my code is doing.
Incidentally the .selector was lifted from the datepicker page on the web:
getDate()
var currentDate = $( ".selector" ).datepicker( "getDate" );
I'll be back when I have a bit more knowledge to be able to understand the answers.

Dave

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.