Search code examples
javascriptphpjqueryupdatingpage-refresh

How to do a page refresh after submitting and executing an update statement to show the updated values?


I have to click on the update button and then do an update in database and a refresh to show the updated values on the same page. These values must be updated in the database as well. I have been trying to do the refresh but it does not work. Need some help and guidance. Is there any other alternative besides page refresh? Can it be done without page refresh?

      <?php
      //initalizing the query
      $id =  $_GET['id'];
      $query = "SELECT * FROM new_default_reports WHERE id = '$id'";
      $result = $conn->query($query);
      $row = $result->fetch_assoc();
      ?>

      <input type="button" id="btnShow" style="overflow:hidden;margin- left:1400px;font-weight:bold;background-color:lightgray" value="Edit Default Reports" />
      <div id="dialog" align="center">
      <form action = "" method="post">
      <label> SQL Statement</label>
      <textarea name="sqlst" style="width:100%;height:40%;" class = "form-control"><?php echo $row['sql_statement']?></textarea><br>
      <label> X axis: </label>
      <input type="text" name="x" class = "form-control" value="<?php echo $row['x_axis'] ?>"><br>
      <label> Y axis: </label>
      <input type="text" name="y" class = "form-control"  value="<?php echo $row['y_axis'] ?>"><br>
      <input type="submit" name = "set" value="Update" style="background-color:darkred;width:100px;color:white;font-weight:bold" onclick="window.location.reload();"/>
      </form>
      </div>

      <?php 
      if (isset($_POST['set'])){
      $query = "UPDATE new_default_reports SET sql_statement ='{$_POST['sqlst']}', x_axis ='{$_POST['x']}', y_axis = '{$_POST['y']}' where id = $id";
      $result = $conn->query($query);
      header("Refresh: 0; url=previewgraphs.php?id=".$id);
      }
      ?>

UPDATED:

  <input type="button" id="btnShow"
   style="overflow:hidden; margin-left:1400px; font-weight:bold; background-color:lightgray" value="Edit Default Reports">
<div id="dialog" align="center">
<form action="previewgraphs.php?id=$id" method="post">
    <label>SQL Statement</label>
    <textarea name="sqlst" style="width:100%; height:40%;" class="form-control">
        <?php echo $row['sql_statement']?>
    </textarea>
    <br>
    <label>X axis: </label>
    <input type="text" name="x" class="form-control"
           value="<?php echo $row['x_axis'] ?>">
    <br>
    <label>Y axis: </label>
    <input type="text" name="y" class="form-control"
           value="<?php echo $row['y_axis'] ?>">
    <br>
    <input type="submit" name="set" value="Update"
           style="background-color:darkred;width:100px;color:white;font-weight:bold">
    <input type="submit" name="submitted" value="Submit the form">
</form>
 </div>

   <?php 
   if (isset($_POST['submitted'])){
  $query = "UPDATE new_default_reports SET sql_statement  ='{$_POST['sqlst']}', x_axis ='{$_POST['x']}', y_axis = '{$_POST['y']}' where id = $id";
  $result = $conn->query($query);

  // make a query to get the updated result and display it on the page
  $select_query = "SELECT sql_statement, x_xis, y_axis FROM new_default_reports WHERE id = $id";
  $select_result = $conn->query($select_query);
  if ($select_result->num_rows == 1) {
      echo "You have successfully updated the database.";
      $row = $select_result->fetch_assoc();
      echo $row['sql_statement'];
      echo $row['x_axis'];
      echo $row['y_axis'];
  }
}
  ?>

Solution

  • Please take updated value from the database after update query.

    Please try this code :-

    <?php 
        if (isset($_POST['set'])){
            $query = "UPDATE new_default_reports SET sql_statement ='{$_POST['sqlst']}', x_axis ='{$_POST['x']}', y_axis = '{$_POST['y']}' where id = $id";
            $result = $conn->query($query);
    
            $select_query = "SELECT * FROM new_default_reports where id = $id";
            $select_result= $conn->query($select_query );
            $row = $select_result->fetch_assoc();
        }
    ?>
    
    <input type="button" id="btnShow" style="overflow:hidden;margin- left:1400px;font-weight:bold;background-color:lightgray" value="Edit Default Reports" />
    <div id="dialog" align="center">
        <form action = "" method="post">
            <label> SQL Statement</label>
            <textarea name="sqlst" style="width:100%;height:40%;" class = "form-control"><?php echo $row['sql_statement']?></textarea><br>
            <label> X axis: </label>
            <input type="text" name="x" class = "form-control" value="<?php echo $row['x_axis'] ?>"><br>
            <label> Y axis: </label>
            <input type="text" name="y" class = "form-control"  value="<?php echo $row['y_axis'] ?>"><br>
            <input type="submit" name = "set" value="Update" style="background-color:darkred;width:100px;color:white;font-weight:bold" />
        </form>
    </div>