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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
                //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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
                //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.

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!! :)

    Reply

Leave a Reply