I would like to implement an ‘add note’ feature on my website that will appear to work effortlessly to the user.
The idea is simple: There will be a textfield next to every item. On-change the text entered into the textfield will automatically be updated in the database, the user won’t have to hit ‘save’ or click any buttons to load or save the note.
(Initially, every item will have a blank text field next to it)
What would be the best way to implement this?
<?php
//Create mysql connect variable
$conn = mysql_connect('samplesource.com', 'example', 'pass');
//kill connection if error occurs
if(!$conn){
die('Error: Unable to connect.' . '<br>' . mysql_error());
}
//connect to mysql database
mysql_select_db("mydb", $conn);
session_start();
$userid = $_SESSION['id'];
$results = ("SELECT * FROM notes WHERE userid='$userid'");
?>
<html>
<head>
</head>
<body>
<textarea style="resize:none; width:300px; height:200px;"> </textarea>
</body>
</html>
This is a call for a jQuery ajax call to update your database. From the ajax call you can hook the onblur event and then update the note in the database, remember that this will cause a lot of backend strain and will cause the MySql server to get bogged down with the constant updates. You may have to look into some kind of caching and buffered save as well to keep this from happening.
Jquery Ajax:
http://api.jquery.com/jQuery.ajax/
Jquery OnChange:
http://api.jquery.com/change/