1

I have a user interface that interacts with some software.

My user interface displays a database with data paths. The user selects the data paths they want to simulate/run, and that sends the data path to the software. After its finished the output is an excel report saved in a folder on the same computer. This is were I'm a bit stuck on how exactly I'm supposed to show the excel report to the user for the specified data path the user selected to simulate/run.

My idea was to create a routine that converts the excel report to pdf and then saves the file path of the report to a database. I then create a div with id="result" to be displayed inside a table row for the specified path selected and loads pdf_report.php which then displays the desired report.

The problem is that every time I reload the page the div tag goes away. I tried using localstorage but it still doesn't get displayed after page reload.

My code is the following:

**Lets the user simulate/run the path selected

Search.php

<?php
...    
  while($row = $result->fetch_assoc()){
            $field1name = $row["test_id"];
            $field2name = $row["path"];
            $field3name = $row["video1_path"];
            $field4name = $row["video2_path"];
            $field5name = $row["video3_path"];
            $field6name = $row["video4_path"];

          echo "<tr>
                  <td> ".$field1name." </td>
                  <td> ".$field2name." </td>
                  <td> ".$field3name." </td>
                  <td> ".$field4name." </td>
                  <td> ".$field5name." </td>
                  <td> ".$field6name." </td>

                  <td><div>
                        <button class='edit' id='" . $row['test_id'] . "'  >Run</button>
                      </div></td>

                  <td><div id='result'>
                    <p></p>
                  </div></td>


                </tr>";
        }
      }else {
        echo '<span style="color:#ff0000;text-align:center;">No Test ID Selected!</span>';
      }
    }


    // Close connection
    mysqli_close($conn);
  ?>
  </table>
</div><br>

<div style="overflow-x:auto;">
<table id=test_data>
  <tr>
    <th>Progress</th>
    <th>Progress Status</th>
  </tr>
  <tr>
    <td><div><progress id='progBar' value='0' max='100'></progress></div></td>
    <td><div><p id='progress-text'></p></div></td>
  </tr>
</table>
</div>


<script type="text/javascript">
var show = localStorage.getItem('showDiv');
    if(show === 'true'){
        $("#result").show();
    }

</script>

<!--Uses jquery to run 3 scripts and displays it in a progress bar-->
<script>
$(document).on('click', '.edit', function(event){

  //set cookie value to 'path'
  var fcookie='mycookie';

  //if button inside row is clicked the path gets saved to a cookie and received in ajax.php
  var test_id = $(this).attr('id');
  if(test_id) {
    var path = $(this).closest('tr').find("td:nth-child(2)").text();

  //Cookie gets saved
  document.cookie='fcookie='+path;


  var $this = $(this);

    //Start of 1st script
      $.ajax({
        url: "ajax.php",
        type:"POST",
        success: function(data) {
          //alert("File 1 Completed")

            $("#progress-text").text("Executing file 1");
            $('#progBar').val(25);

            //Start of 2nd script
            $.ajax({
              url: "ajax2.php",
              type:"POST",
                success: function(data2) {
                  //alert("File 2 Completed")
                  $("#progress-text").text("Executing file 2");
                  $('#progBar').val(50);

                  //Start of 3rd script
                  $.ajax({
                    url: "ajax3.php",
                    type:"POST",
                      success: function(data3) {
                        //alert("File 2 Completed")
                         $("#progress-text").text("Complete");
                         $('#progBar').val(100);

                         //Displays the <div id=result> for the selected data path
                         $this.closest("tr").find("td:nth-child(8)").load("pdf_report.php");


                         event.preventDefault();
                         $("#result").show();
                         localStorage.setItem('showDiv', true);


                      }
                  });
                }
            });
          }
    });
  }
});

 </script>
ian305
  • 25
  • 6
  • Is this report generated for a specific user? If so, you'll need some mechanism to identify the user. Typically, _sessions_ are used for this. – waterloomatt Jul 28 '20 at 12:08
  • No, the all users will get the same report. Its a localhost project and users will access the website through the local network's IP – ian305 Jul 28 '20 at 12:15
  • Curious: How does this work through localhost? Localhost, by definition, is one's own computer. (So each person running it has a different localhost - their own pc) Do you have XAMPP loaded on all users computers? Or is one computer in your company designated as the "webserver" and everyone connects to it? Or, is there only the one computer that everyone physically visits to run the program receive their report? `Waterloomatt` is correct about the usual way to identify the user. You will have to explain very clearly how everyone connects to your app, because I'm not sure that I understand. – cssyphus Jul 29 '20 at 15:08
  • Yes, there will be a computer were the code will be loaded to, designated as the "webserver", and anyone that ones to run/use the user interface will connect to that computer only. – ian305 Jul 29 '20 at 15:55

1 Answers1

0

In javascript, inside document.ready (i.e. runs as soon as the page has been fully loaded), you can have something like:

$(function(){
   const tmp1 = localStorage.get('pdf01');
   const tmp2 = localStorage.get('pdf02');
   if (tmp1 !== undefined){
      $('#dataDiv').append(`<a href="${tmp1}">PDF01</a>`
   }
   if (tmp2 !== undefined){
      $('#dataDiv').append(`<a href="${tmp2}">PDF02</a>`
   }
});

Of course, the above code expects that you already have a div somewhere in your body with the ID dataDiv:

<body>
   <!-- Other HTML code is here... -->
   <div id="dataDiv"></div>
</body>

Note that the above div will be invisible (zero size == invisible) until the javascript sticks the <a> tag inside it. This is a pretty common strategy in web design - universal, really.

If you want to get fancy, you can store JSON in a localStorage variable. JSON is a javascript object converted to a text string (which is why it can be stored in a localStorage variable), and objects, as you known, can contain their own key/value "sub-variables", which can contain their own "sub-variables", etc... So, basically, using objects/JSON you can use just one localStorage variable to store all the settings for your project. Or, to stay simple, you can just use a separate localStorage variable for each bit of information you want to store -- and the end result will be the same.

These References might be helpful:

Setting a var in localStorage from button radio and pass it to other pages

Why aren't my localStorage inputs persistent across pages on my personal computer?

How to retrieve the value from LocalStorage

When is localStorage cleared?

cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • Sorry, maybe i wasn't too clear. As seen in the picture, for practice I ran Test ID 2. And div id=result shows up as a button link "Report" to open up an existing pdf file done in pdf_report.php that was saved in a database. Div id=result is the "td:nth-child(8)" from the table. So any time the user runs the test the button "Report" will show up and provide the report for the test ran. But every time I refresh the page the button "Report" will disappear. Maybe using localstorage is not the best idea because i would potentially have hundreds of test id's and maybe use a database instead – ian305 Jul 29 '20 at 18:37
  • Meaning, make two database tables interact with each other. Data_Path_Table and Pdf_path_Table. If Pdf_path_Table has data inside test_id 2 then demonstrate it by having the "Report" button in the table. Could that be achieved? – ian305 Jul 29 '20 at 18:41
  • Okay, so you've got AMPP installed, which is basically identical to XAMPP *(MariaDB (`XAMPP`) is the latest OSS incarnation of the now-proprietary MySQL (`AMPPS`))*. It appears, then, that the piece you are missing is the need to check your database, and add appropriate buttons, every time the page is refreshed. The values in your database will tell you if the report button should be there or not (per row) - no need for localStorage at all. As to what report any report button should open, that also should be stored on the same row of the database table. And I am unsure why you have two tables? – cssyphus Jul 29 '20 at 19:53
  • I suggest you revisit the two tables design - go for easy over elegant, every time. Re-think what data each table row can/should store. If each row (of the above table image) represents a single test, then the path to the PDF file can be just one more column for each table row. If there are an infinite number of PDFs that might be generated per test, then you can store ALL the paths in one field, as an array (JSONified so it can be stored as text). That is a pretty common approach. – cssyphus Jul 29 '20 at 19:58
  • A couple of clarifications: MySQL === MariaDB. MySQL was taken over by Oracle (now proprietary), so a fork was made to continue the Free/Open-Source version and called MariaDB. They are 99.5% identical, but MariaDB has a few new features. Nothing critical though. LocalStorage and MySQL are two different methods for doing the same thing: preserving data between page refreshes (aka "sessions"). LocalStorage is MUCH simpler, but MySQL can do more. Tons of projects use **both** - localStorage is great for quick&dirty, non-critical, non-secret save/retrieve. – cssyphus Jul 29 '20 at 20:37