Search code examples
javascriptphpajaxcodeignitercodeigniter-3

Php echo date in javascript. Uncaught SyntaxError: unexpected token: identifier


I am trying to send get request to CI controller with data of php date and insert it but keep getting this error:

Uncaught SyntaxError: unexpected token: identifier

this is the error in view-source

date = 17 July 2021;

This is my view and controller

      $(".btn-checkin").click(function() {
         id_user = <?php echo $this->session->userdata('id') ?>;
         date = <?php echo date('j F Y') ?>;
         time = <?php echo date('H:i:s') ?>;
         $.ajax({
            url: "<?= base_url('dashboard/checkin') ?>",
            type: "GET",
            data: {
               "id_user": id_user,
               "date": date,
               "time": time,
               "information": 'check-in',
               "status": 0,

            },
   public function checkin()
   {
      date_default_timezone_set("Asia/Jakarta");
      $datacheckin = [
         'id_user'       => $this->session->userdata('id'),
         'date'          => date('j F Y'),
         'time'          => date('H:i:s'),
         'information'   => 'check-in',
         'status'        => 0,
      ];

      $checkin = $this->absensi->insertcheckin($datacheckin);
}

Solution

  • You have to quote your javascript string variables

    $(".btn-checkin").click(function() {
       id_user = "<?php echo $this->session->userdata('id') ?>";
       date = "<?php echo date('j F Y') ?>";
       time = "<?php echo date('H:i:s') ?>";
       ...