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?
$('#date').html(localStorage.getItem('date') || '');
in your page load to set the value. You are reloading, which means values will be lost.