Jump to content

PHP-Ajax Auto Save


ds111

Recommended Posts

Hey guys

 

I am working on a project, and I have this feature to submit an article. Now, I want it so that when you start typing, every 10 seconds, it saves what you have so far to the database on a temporary table. That way, If your browser crashed, you just log in, go to the submit article page, and there is a link: "Saved Drafts (1)" or similar. you click that, and a popup window shows the titles and a brief preamble of what you've written when that draft was saved.

 

I've looked ALL over the internet. There were like, 2 - -and they were very confusing and didnt work. One of em didnt save to a database, which I NEED it to save to the database.

 

So, if anyone could help me out here, that would be awesome!! Thanks!

Link to comment
Share on other sites

OK, you'll need to use set_timeout(), onkeypress (on the field in which users will be typing), AJAX and PHP.

 

 

Learn basic PHP and AJAX.

 

 

Set a listener for onkeypress on the typing field, and make it do "set_timeout('some_func', 10000);"

 

function some_func() {

    //post data to PHP script to save

    //do it again in 10 seconds

}

Link to comment
Share on other sites

Ok well, I've made my own auto save script. But theres a problem.

if i type some text, followed by TWO enters, then it doesnt save anything after the 2 enters. like so:

 

test

 

Test

 

 

TEST!

 

then only "test <br /> Test" will be saved. I am using tinyMCE as the rich text editor, and am doing a tinyMCE.save() before i $_POST the variables thru ajax to autosave.php. heres how the javascript of the main page with text editor looks like:

 

function autosave()

{

var t = setTimeout("autosave()", 6700);

tinyMCE.triggerSave();

var title = $("#title").val();

// var shortpost = $("#shortarticle").val();

var shortpost = document.getElementById("shortarticle").value;

var fullpost = $("#fullarticle").val();

 

if (title.length > 0 || shortpost.length > 0)

{

$.ajax(

{

 

type: "POST",

url: "autosave.php",

data: "article_id=" + <?php echo $article_id; ?> + "&title=" + title + "&shortpost=" + shortpost + "&fullpost=" + fullpost,

cache: false,

success: function(message)

{

$("#timestamp").empty().append(message);

}

});

}

}

</script>

 

note that: var shortpost = $("#shortarticle").val(); and var shortpost = document.getElementById("shortarticle").value; give the same results...

 

autosave.php:

<?php

//include DB configuration file

$noload = "yes";

include('registry.php');

connect_db();

 

$title = $_POST['title'];

$shortpost = $_POST['shortpost'];

$fullpost = $_POST['fullpost'];

echo $fullpost;

 

$id = (int)$_POST['article_id'];

 

//save contents to database

$query = "UPDATE `post_draft` SET title = '$title', shortpost = '$shortpost', fullpost = '$fullpost' WHERE id = '$id'";

$run_query = mysql_query($query);

echo $query;

 

//output timestamp

echo '<div class="clean-ok">Auto Save system is ready. </div>';

?>

I echo the query, and it shows the problem -- it doesnt save after the 2 enters.

 

so if anyone could help me out, then that would be totally awesome!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.