2

I am practising AJAX by implementing editable HTML table looking like this: table with editable cell

The logic is this:

  1. User clicks on left column (containing integer);

  2. JS script catches its value and by using AJAX adds them to database;

  3. PHP script fetches this value, makes some calculations, adds them to database and shows result in right column (date in <td>).

Now as expected PHP script needs page refreshing to show calculation result (date in <td>). I'm trying to write JS script which will retrieve calculated data from database and immediately show in HTML table's cell. Having troubles with it. Asking for help with this issue.

index.php:

<form id="form-wrap" action="functions.php" method="POST"> 
<select name="select1" required>
  <?php 
  for ($i = 1; $i <= 20; $i++) {
  echo "<option>".$i."</option>"; 
  }
  ?>
</select>
 <br>
 <input type="text" id="name" name="name">
 <input type="date" id="day" name="day">
 <input type="submit" name="button_ok" id="button_ok" value="Write">
 <input type="submit" name="show" id="show" value="Show table">
</form>

functions.php:

<?php
require_once 'dbconnect.php';

$loged = $_POST["name"];
$day = $_POST["day"];

class Count_Add_Class { 

  public function CountDays() {

   global $day, $loged, $day_remind1, $days_before1;

   $currenttime = time();

   $days_before1 = $_POST["select1"];

   // ... make some calculations with user's input ...

   $this->AddDate();

   }

  public function AddDate() {

   global $pdo, $loged, $day, $day_remind1, $days_before1;

    if (isset($_POST['button_ok'])) {

     // ... insert in database user's inpt and calculation result ...
   }
 }

}

$obj = new Count_Add_Class();
$obj -> CountDays();


function Count_days_new_data() {

  global $pdo, $day, $loged, $day_remind1_updated, 

  $stmt = $pdo->prepare("SELECT select1 FROM tablex WHERE name=?");

  $stmt->execute([$loged]);

  $res = $stmt->fetchAll();

     foreach ($res as $row) { 
          $day_remind1_updated = $row['select1'];
     }

   $day_remind1_updated = $day - ($days_before1_updated * 86400); 

   $day_remind1_updated = date('Y-m-d', $day_remind1_updated);     

}

Count_days_new_data();


function Show() {

 global $pdo, $loged, $faq, $day_remind1_updated;

      $stmt = $pdo->prepare("SELECT * FROM tablex WHERE name=?");
      $stmt->execute([$loged]);
      $faq = $stmt->fetchAll();

      $s1 = $_POST['editval'];
      $id = $_POST['id'];

      $stmt2 = $pdo->prepare("UPDATE tablex SET select1=? WHERE 
      id=?");
      $stmt2->execute([$s1, $id]);

      $stmt3 = $pdo -> prepare("UPDATE tablex SET day_remind1=?,WHERE 
      id=?");
      $stmt3->execute([$day_remind1_updated, $id]);

      require_once 'table.php';   

} 

Show();

?>

table.php:

<div id="day-data">
<tbody>
<?php
    foreach($faq as $key=>$value) {
?>

    <tr class="table_row">
    <td><?php echo $value['name']; ?></td>
    <td aria-label="First" contenteditable="true" onBlur="saveToDatabase(this,'select1','<?php echo $faq[$key]["id"];
?>')" onClick="showEdit(this);"><?php echo $faq[$key]["select1"]; ?> 
    </td>
    <td><?php echo $value['day_remind1']; ?></td>
    </tr>

<?php
   }
?>

edit.js:

function showEdit(editableObj) {
   $(editableObj).css("background","#FFF");
 } 

function saveToDatabase(editableObj,column,id) {
  $(editableObj).css("background","#FFF url(loaderIcon.gif) no-repeat 
  right");
  $.ajax({
    url: "functions.php",
    type: "POST",
    data:'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,
    success: function(data){
      $(editableObj).css("background","#FDFDFD");
    }        
   });
}

show.js: (inspired by this answer)

$(".tbl tbody").on("click", "tr", function() {
  var id = $(this).find("td")[0].text(); // gets the first td of the 
  clicked tr
  var value =  $(this).find("td")[1].text()
  $.ajax({
    url : "table.php",
    dataType: "text",
    success : function (data) {
        $(".day-data").html(data);
     },
     error: function (data) {
        (".tbl").html('No such file found on server');
     }
    });
  });

When user clicks on HTML-table left column it is higligted well; when he changes it, data is addiing to database well. Now when page is refreshed new data is calculated, added to database and shown in HTML-table's right column.

I suppose problem is in show.js. Need some help in correcting this script!

Update1: Added illustartion of my task's logic: enter image description here

1 Answer 1

1

It is unfortunate, that you have everything in the functions.php file. It doesn't just count the values, it also renders the table. So when you call the function saveToDatabase, the request that you make to functions.php (and i understand it coorectly) already returns the table with the updated data (as argument of the success function). Try removing show.js and changing the success function content in edit.js from this:

$(editableObj).css("background","#FDFDFD");

to this

$(editableObj).css("background","#FDFDFD");
$(".day-data").replaceWith(data);
Sign up to request clarification or add additional context in comments.

3 Comments

Tryed, unfortnately with no effect. Maybe I should move my PHP Show() function to table.php script which renders the table? But the table.php and functions.php are linked between each other by require statement, so I doubt that this will help. What is fortunate true way of functions location (assuming procedurial no-framework approach)?
I noticed now, that day-daya is not a class, it is an id. So instead of $(".day-data").replaceWith(data); (what I wrote in the answer), try $("#day-data").replaceWith(data); without show.js
Hi! Sorry for delay with answer. Tryed your advice, now it seems working fine, but with one point -my table is shown again and again when I click on editable cell...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.