0

I am building a Datepicker (credits css/js: gijgo.com) that contains a "change" event. It allows to activate a code when the user selects a date:

<!-- Date picker script-->  
     <script>
        $('#datepicker').datepicker({ 
            calendarWeeks: true, 
            modal: false,
            header: true,           
            footer: false, 
            format: 'yyyy-dd-mm',
            weekStartDay: 1,
            width: 133,
            change: function (e) {
            alert('Change is fired');
         }
        });
     </script>

My PHP file would use the date selected as an argument to fetch data from the MySQL table

<?php
$argument1 = $argv[1];

How can I insert this PHP so that it is called "change"? Is it actually a good practice to code this way?

I just starting coding with PHP/HTML scripts. Apologies for my lack of knowledge.

0

4 Answers 4

0

you can send the data into PHP with AJAX

Sign up to request clarification or add additional context in comments.

Comments

0

use Change event of Javascript and call a function and in that function you can use jQuery with ajax to pass your datepicker data to php file, you can use get or post method in ajax call and use $_POST and $_GET global variables in that php file to receive data and then write your usual mysql code to fetch data and return it

Comments

0

You cannot set php variables using Javascript because php runs on the server but Javascript runs in the browser. You'll need to send the data back to your server so that server updates your variable. The preferred way would be to use AJAX or else you can create a hidden input and load the value from datepicker into it. That way, you can trigger some form submit to send the data to the server.

<input type="hidden" id="datepickerVal" />

<!-- Date picker script-->  
 <script>
    $('#datepicker').datepicker({ 
        calendarWeeks: true, 
        modal: false,
        header: true,           
        footer: false, 
        format: 'yyyy-dd-mm',
        weekStartDay: 1,
        width: 133,
        change: function (e) {
          $('#datepickerVal').val(e.newDate); //assuming e.newDate gives you date value
        }
    });
 </script>

1 Comment

I have done the following:
0

I think I found the answer:

In the main html section, I have a added a URL parsing

<!-- Date picker script-->  
 <script>
    $('#datepicker').datepicker({ 
        calendarWeeks: true, 
        modal: false,
        header: true,           
        footer: false, 
        format: 'yyyy-dd-mm',
        weekStartDay: 1,
        width: 133,
        change: function redirect () {
              // (B1) APPEND THE JS VARIABLES AS QUERY STRING
              var src = "get_date.php";
              src += "?datepicker=" + document.getElementById("datepicker").value;
              src = encodeURI(src);
             
              // (B2) REDIRECTION
              window.location.href = src;
              return false;
            }
    });
 </script>

And then in the PHP File, I have added the following:

<?php

   $val = $_GET["datepicker"];

   // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
   // Check connection
    if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
    }

     $sql = "SELECT WeekID, DateNum, DayNum, Day_Desc_Short, Day_Desc_Long FROM datesTimesheet where DateNum = '$val'";

    $result = $conn->query($sql);


     $row = mysqli_fetch_array($result);

    echo $row["WeekID"];

      $conn->close();

  ?>

That actually gave the data I needed. The new problem I have is that the value of the date in the datepicker is delivered yyyy-mm-dd. MySQL is taking it as US format (yyyy-dd-mm) so working on that now.

Nonetheless, is my method correct? Is it secure? etc...Eventually for now, I am just learning, so not focusing on security, but would like to know as to improve in the right direction.

1 Comment

Also solved the format directly from the script...did it wrong there.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.