SPServices and PreSaveAction

Aug 18, 2011 at 6:28 PM

Hello,

In my NewForm (WSS3), I'm hiding some fields using jquery and running some SPServices for lookups etc. Now I'm trying to use PreSaveAction to copy an entry field, encode() special characters, then put it into the hidden field and save.

Problem is that PreSaveAction doesn't fire at all. Any suggestions for how I might get it working? Does SPServices have its own version of this function? Even my test code doesn't fire the alert.

$(document).ready(function() {
   function PreSaveAction() {
      alert("test");
      //spservices stuff cut out
   }
}

Am I missing something?

Thanks,

Jason

Coordinator
Aug 18, 2011 at 6:38 PM

You don't want PreSaveAction to be wrapped in $(document).ready, but I don't think it should matter. PreSaveAction is a built-in SharePoint function which doesn't do anything by default. If you add your own script to your own version of it, it will fire when you try to save the form.

Check out my blog post:
http://sympmarc.com/2009/05/28/validation-on-sharepoint-forms-part-four/

M.

Aug 18, 2011 at 6:53 PM

Wow, ok that was easy! Moving it out of the $(document).ready did the trick. I had to alter the code a bit to pass along to the SPServices stuff but all is well now :)

Thanks again,

Jason

Aug 18, 2011 at 7:04 PM

Oh I just noticed. It looks like when using the presaveaction to populate a hidden field, it doesn't get obtain the data on save. The new data only appears to save when the field is visible. Any way to get around this?

Jason

Aug 19, 2011 at 12:19 PM

It depends on the field you are updating and how you are updating it. I do this a lot with SPServices and it works fine. What fields are trying to update and how are they "hidden"?

Coordinator
Aug 19, 2011 at 12:22 PM

Dan's right, you can make this work. Fields which are disabled won't be sent with the commit, and there are some other instances as well. What you'll need to do is re-enable the field in the PreSaveAction so that it is submitted.

M.

Aug 19, 2011 at 12:36 PM

Thanks for the responses! Below is my current PreSaveAction... as a workaround, I'm just unhiding the field prior to save. Does hiding the field disable it as well?

function PreSaveAction() {
	if (queryStringVals["ContentTypeId"] == '0xxxxxxxxxx') { 
		$('tr:has(input[title=custNameEncoded])').not('tr:has(tr)').show();
		$("input[Title='custNameEncoded']").val(escape($("input[Title='Customer Name']").val()));
		return true;
	}
}

Basically its just a plain text field and I've used the hide() command for this and several other fields.

Jason

Coordinator
Aug 21, 2011 at 2:29 AM

No, hiding the field won't disable it; that's a different attribute.

Remember that PfreSaveAction won't be called until your user clicks the OK or Save button.

Since you haven't posted many details, it's hard to know what issues you might be causing yourself.

M.