I am trying to implement an autosave functionality using ajax and php. Here is my script:
function autosave() {
alert($("#myform").serialize());
$.ajax({
url: "form_creator.php",
data: $("#myform").serialize(),
type: 'POST',
success: function(data){
if(data && data == 'success') {
alert("saved");
// Save successfully completed, no need to do anything
}else{
alert(data);
// Save failed, report the error if you desire
// ....
}
}// end successful POST function
}); // end jQuery ajax call
// end setting up the autosave on every form on the page
}// end function autosave()
setInterval(autosave, 5000);
The problem is that I am getting different strings in the first alert and in the second one (in else section). In the first alert, the string seems to be correct with key=value&key2=value2.... but the second alert in else section gives me all html page. Is this correct behaviour? The autosave functionality doesn't work... Am I missing something?
<?php if((isset($_POST['sub'])) || (isset($_POST["autosave_check"])))
{ //save to db
}else{
?>
<form action="" method="post" id="myform" class="ajax_form" name="myform" enctype='multipart/form-data'>
<ul class="sorting dd-list" id="my_list">
//generate inputs
</ul>
<input type="hidden" name="chbox" class='hdbox' id="chbox" value="" />
<input type="hidden" name="save" value="<?php echo $idn?>" />
<input type="hidden" id="autosave_check" name="autosave_check" value="1">
<br class="clear" /> <br />
<div class="creator-buttons">
<h2 class="subheader-creator"> Akce </h2>
<table>
<tr>
<td>
<a href="javascript:;" class='radiusz addchbox' id="ch" value="Checkbox field"><?php echo $langa['formc'][10];?>
</a>
</td>
<td align='right'>
<input type="hidden" id="paths" value="<?php echo $patH?>" />
<input type="submit" name='sub' value='<?php echo $langa['formc'][13]?>' id="saveT" class='dugme1 submitforms' style="float:right;" />
</td>
</tr>
</table>
</div>
</form>
<?php } ?>
Its quite long code so just some parts.. Saving on button click works perfectly as for any other form.