1

I've got a table of data with clickable rows which will lead to another page based on the row that was selected. To get the row, I am using a form and post the form to another page to pass the variable to php. Now, the problem is once I refresh the page my info is gone...

How can I avoid that?

  <?php 
    $id = $_SESSION["uid"];

    $sql = "select * from std_cources join courses where std_cources.std_id = '$id' and courses.course_id = std_cources.course_id";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
      while($row = $result->fetch_assoc()) {
        echo '
              <div class="col-md-4">
              <span class="fa-stack fa-4x">
              <i class="fa fa-circle fa-stack-2x text-primary"></i>
              <i class="fa fa-shopping-cart fa-stack-1x fa-inverse"></i>
              </span>
              </br>
              <p style="color:white;">'.$row["course_name"].'</p>
              <a href="javascript: functionTest('.$row["course_id"].')" style="color:white;">Check forthe exams </a>           
              <form name="myform" id="'. $row["course_id"].'"  action="checkMarks.php" method="post">
              <input type= "text" value ="'. $row["course_id"].' " name="test" style="visibility: hidden;"></input>
              </form>
              </br>                 
            </div>';
      }
    }
?>

   <script >
     function functionTest(form_id) {
       document.getElementById(form_id).submit();
     }
   </script>

enter image description here

I am retrieving names of few courses from database and put them in a table. Then I can click on each one of them and the form submission will be triggered and sends info to the next page and in the next page I get the given info and retrieve info from database again. However, on the second page, if I refresh the page I get

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Danial Kosarifa
  • 1,044
  • 4
  • 18
  • 51

1 Answers1

4

When you refresh a page that has received data from an earlier POST request, the refreshed page won't have that data unless another POST request is made. This can be done in the window object's DOMContentLoaded or load event.

Or, you can make the initial request for the data via a GET request. This will send whatever data you are sending to the server as part of your request as a query string appended to the URL. If you refresh the page, that query string will persist. This will only work if the data you are attempting to get comes from the server-side processing of the current page and not some other URL entirely.

Lastly, POST requests are for requests that change the state of the server (insert, update and delete type of operations). GET should be used for read operations.

Without your code, there's really not much more to offer.

EDIT:

Now that you have posted your code, I would suggest spending some time and cleaning up the HTML string that is sent back from your .php file. There is no such tag as </input> and you should remove the inline HTML event attributes (onclick, etc.). Here's why. Don't use javascript:... either (for many of the same reasons as in the link.

Lastly, I would suggest you change this from a form submission to an AJAX GET request, which will allow you to stay on the same page and keep the currently loaded data.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71