1

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.

  • Your $_POST check [may not be enough.](http://stackoverflow.com/questions/3496971/check-if-post-exists) – Mike Cluck Jul 22 '16 at 21:06
  • you're passing a function as the data parameter for your `.post()` call. you should be passing the actual form data, e.g. `$.post('foo.php', {diaryInput: $('textarea').val() });` – Marc B Jul 22 '16 at 21:08
  • Ok thanks, I've tried both of these. Now the code isn't being deleted but it isn't updating the database. (Also see last section of original post, I forgot to add that in) – Owen Ffrancon Jul 22 '16 at 21:26

0 Answers0