Fields Missing from jQuery Post – Serialize Data

Hey everyone,

I was using jquery’s serialize method to post completed form data only to find that a number of my fields were missing.


                //Display loader and disable forms
		disable_form_fields(true);

                //Post via ajax
		$.ajax({
			type: 'POST',
			url: 'uploads/add',
			data: $(form).serialize(),
			success: function(data, text_status, oHTTP){
				handle_form_response(data, text_status, oHTTP, $(form).data('file_id'))
			},
			error: function(){

				//Hide loader etc
				set_form_loading(false);

				//Unspecified error
				alert('An error has occurred.');
			},
			dataType: 'json'			
		});

Thanks to this stackoverflow post I realised that the data wasn’t being serialised because I’d disabled most of the fields beforehand. This was done in order to prevent the user from changing the values. To get around this I simply had to serialise the data BEFORE disabling anything.


                //Post via ajax
		$.ajax({
			type: 'POST',
			url: 'uploads/add',
			data: $(form).serialize(),
			success: function(data, text_status, oHTTP){
				handle_form_response(data, text_status, oHTTP, $(form).data('file_id'))
			},
			error: function(){

				//Hide loader etc
				set_form_loading(false);

				//Unspecified error
				alert('An error has occurred.');
			},
			dataType: 'json'			
		});

               //Display loader and disable forms - DO THIS AFTER SERIALISING
		disable_form_fields(true);

Let me know if you have any issues.

Advertisements

2 thoughts on “Fields Missing from jQuery Post – Serialize Data

  1. Michael

    Hey! This post helped me with a problem that was introduced presumably by a jQuery update. My serialize() call was no longer picking up disabled options. Enabling them before sending the data solved my issue – thanks for posting!! 🙂

    Like

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s