1

I have table which td is contenteditable. To update the value of td in my database I decide to use localStorage. Once the button save is click the inputted value in td will save to localStorage and ajax will get it's value to replace in a column in my database.

I look at the Application in Google Chrome to check if it is saving to localStorage and it does!

but when page reload the value in td doesn't change and update. What am I missing in my code?

$(document).ready(function() {
             $('#btnSaveFindings').click(function(){
                 var id = $(this).data('id');
                 var employeeName = document.getElementById('employeeName').value;
                 var edit = $('#date').html(); // get this field value
                 localStorage.setItem('date', edit);
                 var storeDate = localStorage.getItem('date');
                     $.ajax({
                         url: 'update_assesment.php',
                         type: 'post',
                         data: {
                             'id' :id,
                             'date': storeDate,
                             data: edit,
                             'employeeName' :employeeName   

                        },
                         //datatype: 'html',
                         success: function(){
                            alert(edit);
                         location.reload(); 
                        }
                     });
            });
        });

'date' is one of my td just to be the sample.

update_assessment.php

 <?php
$id = $_REQUEST["id"];
$employeename = $_REQUEST["employeename"];
$storeDate =  $_REQUEST["date"];
$emp_name = $_REQUEST["emp_name"];
//$id_edit = $_REQUEST["id"]

try {
    $pdo = new PDO('mysql:host=localhost:3306;dbname=****;', '****', '****' );
    $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    $pdo->query( 'SET NAMES UTF8' );
    $stmt = $pdo->prepare(
        "UPDATE tbl_assessment
        SET
            `employeename` = :employeeName,
            `date` = :storeDate
        WHERE
            `id` = :id AND employeeName = '{$_SESSION['emp_name']}'
        "
    );
    $stmt->bindValue(':id',$id,PDO::PARAM_STR);
    $stmt->bindValue(':employeeName',$employeeName,PDO::PARAM_STR);
    //$stmt->bindValue(':teamCode',$teamCode,PDO::PARAM_STR);
    $stmt->bindValue(':storeDate',$storeDate,PDO::PARAM_STR);


    //$stmt->bindValue(':id',$id,PDO::PARAM_INT);
    $stmt->execute();

    header('location:./');
}catch ( PDOException $e ) {
    var_dump( $e->getMessage() );   
}

$pdo = null;

?>

I really don't know it is not getting value in localStorage. Any help?

4
  • how do you set the value onload ? Jun 27, 2019 at 1:25
  • where is session_start(), or whats that $_SESSION var doing there? Jun 27, 2019 at 1:26
  • you will need to add something like $('#date').html(localStorage.getItem('date') || ''); in your page load to set the value. You are reloading, which means values will be lost. Jun 27, 2019 at 1:32
  • now it gets the value in localStorage. But it doesn't update value in my database. The value on that td must be what on the localStorage. TD's has column in my database and has a value of null. So if the user will input data, it will become the value in that column in db
    – Gelxxxx
    Jun 27, 2019 at 1:57

1 Answer 1

0

You don't assign the stored value to the td when loaded, try to add the following code to the first place of $(document).ready(function() { /*add it here*/ });:

$('#date').html(localStorage.getItem('date') || '');
3
  • now it gets the value in localStorage. But it doesn't update value in my database. The value on that td must be what on the localStorage.
    – Gelxxxx
    Jun 27, 2019 at 1:55
  • What do you mean? Do you want to get the value from localStorage and then update it to the database? If so, just $('#btnSaveFindings').trigger('click');.
    – Pinetree
    Jun 27, 2019 at 2:09
  • yes,because thats the first way that i think to update database in making td contenteditable... for front-end editing. that code will keeps button to be click.
    – Gelxxxx
    Jun 27, 2019 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.