4

I am quite ok with PHP, but a total noob with jQuery, and got stuck with autosaving form data.

The autosave function gets called every 30 seconds in dummy.php. I'm sending the serialized form data for processing (--> database) to savetest.php.

At this moment, I am stuck with this question:

How do I get savetest.php to 'listen' to incoming data and react to it?

At this moment, I get the alert 'Oh no!' ( = no success) every 30 seconds.

Here's my shortened sample code:

dummy.php, snippet 1 (HTML & PHP):

<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <script type="text/javascript" src="../../_include/jquery-1.9.1.min.js"></script>
</head>
<body>
 <h1>My Form</h1>
 <form name="form1" id="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>"> 
 <input type="radio" name="item_2_1" id="c_2_1_0" value="0" />
 <input type="radio" name="item_2_1" id="c_2_1_1" value="1" />
 <input type="radio" name="item_2_1" id="c_2_1_2" value="2" />
 <input type="radio" name="item_2_1" id="c_2_1_3" value="3" />
 <input type="checkbox" name="item_3_1[]" id="c_3_1_0" value="yes" />
 <input type="checkbox" name="item_3_1[]" id="c_3_1_1" value="no" />
 <input type="text" name="item_4_1" id="c_4_1_0" />
 </form>

dummy.php, snippet 2 (jQuery):

<script type="text/javascript">
 function autosave() {
 jQuery('form').each(function() {
 jQuery.ajax({
 url: 'savetest.php',
 data: {
 'autosave' : true,
 'formData' : jQuery(this).serialize()},
 type: 'POST',
 success: function(data){
 if(data && data == 'success') {
 alert("OK!");
 }else{
 alert("Oh no!");
 }
 }// end successful POST function
 }); // end jQuery ajax call
 }); // end setting up the autosave on every form on the page
 }// end function autosave()
 jQuery(function($) {
 setInterval(autosave, 30 * 1000);
 });
</script>

and this is savetest.php:

if (isset($_POST)) {
var_dump($_POST);
exit();
} else {
 echo 'Hi, no $_POST.';
}

unfortunately, still the unsuccessful alert... and savetest.php dumping array(0){} :-(

asked Aug 19, 2013 at 23:40
7
  • If you are using AJAX, you really should have it calling a separate backend script that is different from the calling page. Commented Aug 20, 2013 at 0:24
  • Why data == 'success'? data is the server response, in this case the var_dump not the string 'success'. Commented Aug 21, 2013 at 23:15
  • @xmarston ok, can I echo 'success' after the var_dump()? Commented Aug 21, 2013 at 23:17
  • @michi no, because the server response won't be only 'success', it will be var_dump() and 'success', try only echoing 'success'. Commented Aug 21, 2013 at 23:20
  • 1
    @michi (1) You can check what the php file has received echoing var_dump($_POST) like you had before and in the success function of the ajax doing a console.log(data). You can see this in the Firebug console(Firefox) or in Chrome console. (2) There are many ways, with your code you will autosave all existing forms in the web, if you only want to autosave a certain form and that form has an id the better way will be doing $('#formid) or before the ajax call, check if the id of the form is the one that you want doing if($(this).prop('id') == 'form1'). Commented Aug 22, 2013 at 5:58

1 Answer 1

2
if(isset($_POST)):
 //processing to save autosave
else:
 //the rest of your form code and jQuery stuff
endif;

This will check if the data is posted, the //rest of your data should represent All other code generated by the page that does not do any processing.

Also, may I suggest cleaning up that data attribute?

data: {
 'navigation': 'save',
 'autosave' : true,
 'formData' : jQuery(this).serialize()
}

Then on the server side you can access it just like normal, $_POST['navigation'] and $_POST['formData'] contains the array of name=value combinations.

Moreover, your function should be within $(document).ready, or like the following:

jQuery(function($){
 //the rest of your code
});

jQuery(function($) will run as $(document).ready and will alias the $ symbol as jQuery while within that function.

This should cover all bases.

answered Aug 19, 2013 at 23:44
Sign up to request clarification or add additional context in comments.

4 Comments

thank you for all those suggestions. see my edit --> unfortunately, I still get the Oh no alert... any idea why?
@michi did you make the adjustment to the data parameter? it still looks the same, also, you might as well just do if(isset($_POST)) for the time being, and then dump it back, like this -> echo '<pre>', print_r($_POST, TRUE),'</pre>';
@michi Keep me updated so I can make sure you've resolved this issue.
see my edits - I send the data to another php file now, but still no success.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.