I tried to write a script mixing something I found on the internet in order to autosave a form data to the mysql db... Something went wrong cause the script it is able to insert but for some reason not to update so every 20 sec (the time I set up) is generating a new row... Can anyone help me to find and solve the issue?
Here is the code:
<?php
session_start();
unset($_SESSION['article_id']);
require_once('xajax/xajax_core/xajax.inc.php');
$xajax = new xajax();
function savetodb($form) {
$title = $form["title"];
$editor = $form["editor1"];
//$host = 'localhost';
//$username = 'my_user';
//$password = 'my_pass';
//$database = 'test_db';
//$connect = mysql_connect($host, $username, $password);
//mysql_select_db($database, $connect);
if ($_SESSION['article_id']=="") {
$sql = "INSERT INTO draft (`title`, `content`) VALUES ('$title', '$editor')";
$result = mysql_query($sql, $connect);
$idlast = mysql_insert_id($connect);
$_SESSION['article_id'] = $idlast;
} else {
$article_id = $_SESSION['article_id'];
$sql = "UPDATE draft SET `title`='$title',`content`='$editor' WHERE `id`='$article_id'";
$result = mysql_query($sql, $connect);
}
// Instantiate the object xajaxResponse
$objResponse = new xajaxResponse();
$objResponse->assign("autosavemsg","innerHTML", "<br />Record saved to database successfully!");
$objResponse->alert('Done!');
return $objResponse;
}
//$xajax->register(XAJAX_FUNCTION,'savetodb');
$xajax->registerFunction('savetodb');
$xajax->processRequest();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<?php $xajax->printJavascript('xajax'); ?>
</head>
<body>
<form name="form" id="form" enctype="multipart/form-data">
<?php
echo '<label>Title</label><input type="text" name="title" id="title"><br /><br />';
echo '<textarea name="editor1"></textarea>' ;
?>
<input type="button" name="save" onclick="xajax_savetodb(xajax.getFormValues('form'));" value="Save to Database">
</form>
<div id="autosavemsg"></div>
<script language="Javascript">
//Interval
var AutoSaveTime=20000;
//time object
var AutoSaveTimer;
SetAutoSave();
function SetAutoSave() {
AutoSaveTimer=setInterval("xajax_savetodb(xajax.getFormValues('form'));",AutoSaveTime);
}
</script>
</body>
</html>
Unfortunately is not even give to me the alert "done" or the message that I set..
thanks in advance
I'm not familiar with xajax
but referring to my logic, I think you are doing it wrong.
I think you have to have two different files, one is your HTML
and javascript
and other one is your server-side code which is responsible for inserting or updating the db
.
right now as I think you are calling the file itself with ajax
and in the third line of the code you have unset($_SESSION['article_id']);
which will unset the session variable $_SESSION['article_id']
each time you call the ajax
.
so I think you have to make two different files.
file1.php you have your html
and javascript
files in it and also have the unset($_SESSION['article_id']);
line in it.
files2.php you have your php
and db related functions
in it and delete the unset($_SESSION['article_id']);
line from it.
you also have to set in file1.php that your ajax
should call file2.php
I think this will do the trick
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.