autosaving form data with jquery and php -
i quite ok php, total noob jquery, , got stuck autosaving form data.
the autosave function gets called every 30 seconds in dummy.php. i'm sending serialized form data processing (--> database) savetest.php.
at moment, stuck question:
how savetest.php 'listen' incoming data , react it?
at moment, alert 'oh no!' ( = no success) every 30 seconds.
here's 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 autosave on every form on page }// end function autosave() jquery(function($) { setinterval(autosave, 30 * 1000); }); </script> and savetest.php:
if (isset($_post)) { var_dump($_post); exit(); } else { echo 'hi, no $_post.'; } unfortunately, still unsuccessful alert... , savetest.php dumping array(0){} :-(
if(isset($_post)): //processing save autosave else: //the rest of form code , jquery stuff endif; this check if data posted, //rest of data should represent other code generated page not processing.
also, may suggest cleaning data attribute?
data: { 'navigation': 'save', 'autosave' : true, 'formdata' : jquery(this).serialize() } then on server side can access normal, $_post['navigation'] , $_post['formdata'] contains array of name=value combinations.
moreover, function should within $(document).ready, or following:
jquery(function($){ //the rest of code }); jquery(function($) run $(document).ready , alias $ symbol jquery while within function.
this should cover bases.
Comments
Post a Comment