javascript - How to auto-save and auto-update textarea

850

I'm currently attempting to create a test-website based on the "Secret Diary" project of a web developer course. I'm trying to create a page that saves all of the notes written into a textbox, and displays them when I log in again. Almost everything works - I can start a session and display the saved text when I log in, but the box is deleting the textbox's saved data when the page is loaded. I know that there are better ways of storing the info, I'm just looking for how to get this method to work. This should be all of the relevant code:

mainpage.php:

<?php include("updatediary.php"); ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

<form method="post">
<textarea class="form-control"><?php echo $diary; ?></textarea> 
</form>

<script>
$("textarea").keyup(function() {
$.post("updatediary.php", function(){diaryInput:($("textarea").val());} );  
});
</script>

updatediary.php:

<?php
include("connection.php"); 

$query = "SELECT content FROM users WHERE id='".$_SESSION['id']."' LIMIT 1";
$result = mysqli_query($dbCon,$query);
$row = mysqli_fetch_array($result);
$diary = $row['content'];

if ($_POST['diaryInput']!="") { 
$updateQuery = "UPDATE `users` SET `content`='".mysqli_real_escape_string($dbCon, $_POST['diaryInput'])."' WHERE id='".$_SESSION['id']."' LIMIT 1";


if (mysqli_query($dbCon, $updateQuery)) {
echo "saved";
} else {
echo "not saved";
};  
}

?>

connection.php:

$dbCon = mysqli_connect("localhost", "owenxwfg_admin", "(password)", "owenxwfg_users");

Any help would be awesome. I personally think that there's a problem with my $.post part.

People are also looking for solutions to the problem: Need Help Optimizing PHP script pulling Site Name of test sites

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.