...Some form gets submitted...
$.("form").submit(function() {
saveFormValues($(this), "./../.."; }
function saveFormValues(form, path) {
var inputs = getFormData(form);
var params = createAction("saveFormData", inputs);
var url = path + "/scripts/sessions.php";
$.post(url, params);
}
The weird thing is that if i add a function to the$.post(url, params, function(data) {
alert(data); }
I get a blank alert statement.
Within scripts/sessions.php i have a function to save whatever the $_POST information is to a file, and the sessions.php never saves this saveFormValues call. It never shows up to the file. But if i keep trying to get it to save, about once every 15 will actually allow it to be saved. This leads me to believe that the forms POST is somehow blocking this value saving post. Any help?
You need to cancel the original submit by returning false from the handler. Update: If you want the original form submission to continue, then you can remove the handler and re-submit the form from the post callback.
$.("form").submit(function() {
saveFormValues($(this), "./../..");
return false; // cancels original submit and allows AJAX post to complete
});
function saveFormValues(form, path) {
var inputs = getFormData(form);
var params = createAction("saveFormData", inputs);
var url = path + "/scripts/sessions.php";
$.post(url, params, function() {
$('form').unbind('submit').trigger('submit');
});
}
add a return false;
to the submit function to prevent the form from submitting:
$.("form").submit(function() {
saveFormValues($(this), "./../.."); return false });
UPDATE:
if you want to later submit the form, keep a variable to indicate if it can be submitted:
var canSubmit = false;
$.("form").submit(function() {
if(!canSubmit)
{
saveFormValues($(this), "./../..");
return false;
}
});
and later:
$.post(url, params, function(data) {
alert(data);
canSubmit = true;
$.("form").submit();
}
So instead of doing a $.post i tried doing a $.ajax with asynch set to false. It saves the form values every ... like 3/5 times... any new suggestions?
So i decided to create an associative list of items that will not be saved from a form.
$nosave = array("password" => true, "confirmPassword" => true, "descriptionCount" => true, "detailedDescriptionCount" => true, "submit" => true, "action" => true, "p" => true); //Then i decided to go through the $_POST information and save it to the SESSION info.
foreach ($_POST as $key => $value) {
if ($key != $id && !array_key_exists($key, $nosave)) {
//saves the value to the form.
$_SESSION[$id][$key] = $value;
}
}
//The $id is a hidden input.. name="formId" value="someUniqueId" That way whenever
//the form info is saved, its saved under a unique area and can be easily unset.
Instead of doing it through jQuery, i just called a function that roughly does all of that in my sessions.php file.