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
https://forums.phpfreaks.com/topic/131084-php-ajax-auto-save/
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
https://forums.phpfreaks.com/topic/131084-php-ajax-auto-save/#findComment-680615
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
https://forums.phpfreaks.com/topic/131084-php-ajax-auto-save/#findComment-684243
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.