1

I'm currently working on an Employee Work Monitoring, where their output as encoder will be monitored by recording when they start and finished the needed documents.

After they finished encoding they save the final data in a server based on the category. So the data is not stored in one folder only, it has multiple folders.

example : data1 :

server/category1/000000-1/000000-1-final data.pdf
server/category2/000000-2/000000-2-final data.pdf

As you can see in this sample image, before staff can finished one output, they have to click first a button. This button will check or search for the data (where filename is same as the work code) in server if it exist. Reason for this is to make sure they encode correctly the standard filename for the data. They cannot finished one output if filename is incorrect/doesn't exist.

actual table

How can I achieve this kind of logic? Where it must search for different folder to check the data.

If it exists then update the SQL table by timestamp as value of finished column.

This is what I've tried for the function of the button:

  var id = $(this).attr('id').split('-')[1] 
  var work_code = document.getElementById('work_code-'+id).value;

  $.ajax({   ///  CHECK PHP SCRIPT IF TRUE
    type:'post',
    url: 'checkData.php',
    data:{
      work_code: work_code
    },
    success: function() {    /// if file found
      $("#CheckButton").css("display","none"); //  hides the button when click
      $('#Finished-'+id).html(CurrentTimestamp)  // make column value to timestamp
      var value = (CurrentTimestamp);
      var column = $("#Finished-"+id).attr('id').split('-')[0]              
      $.ajax({  /// if file found then update value of sql
        type:'post',
        url: 'editable.php',
        data:{
          value: value,
          id: id,
          column: column
        }
      }).done(function(data){
        GrowlNotification.notify({
          title: 'DATA FOUND!',
          description: 'Plan Successfully Finished',
          showProgress: false,
            image: {
              visible: true,
              customImage: 'Images/success.png'
            },
            type: 'success',
            position: 'top-right',
            closeTimeout: 2000
         });  
         setTimeout(function(){
           sessionStorage.reloadAfterPageLoad = true;
           window.location.reload(); // then reload the page.(3)
         }, 2500); 
       })
    }, 

    error: function() {   // if file not found
      swal("DATA NOT FOUND!",  "Please double check data's filename.", {
        icon: 'error',
        button: false,
        timer: 1900,
      });
    }
  }); 
})

This is the script for checkData.php: (This part is what I don't know what to put in order to check the data in server.)

<?php
$server = 'http://10.000.00.00:0000/';

$cat1 = $server.'/category1'.$_REQUEST["work_code"].'/'.$_REQUEST["work_code"].'-final data.pdf';
$cat2 = $server.'/category2'.$_REQUEST["work_code"].'/'.$_REQUEST["work_code"].'-final data.pdf';
// condition to check if file exist

if (fopen($cat1,"r") || fopen($cat2,"r")) {
    return true;
}else{
    return false;
}

?>
kmoser
  • 8,780
  • 3
  • 24
  • 40
NewbieKid
  • 87
  • 7

1 Answers1

0

Your checkData.php script is returning a Boolean value (true or false) but it is not returning a complete HTTP response. Your script should return a complete HTTP response, and encode the results in something like JSON format, like this:

$data = [ 'exists' =>
  (fopen($cat1,"r") || fopen($cat2,"r"))
];
header('Content-Type: application/json');
echo json_encode($data);

Then, your JS .done() function should parse the returned JSON response, examine the value returned by the server, and respond accordingly, like this:

done(function(data){
  if (data.exists) { // 'exists' is the key returned by PHP
    console.log('The file exists!');
  } else {
    console.log('The file does NOT exist!');
  }
}
kmoser
  • 8,780
  • 3
  • 24
  • 40
  • Your provided JSON Format isn't working... can you provide how can I return complete HTTP response base on my code? – NewbieKid Jul 27 '20 at 05:02
  • Sorry, I had a typo in my PHP code. `:` should have been `=>`. I have changed the PHP example. You may also have to change your `$.ajax` call to accommodate receiving JSON data. The links I provided in my answer should help with that. – kmoser Jul 27 '20 at 13:46