[jQuery] Re: Form Plugin: editing/adding data before send
I am using the jQuery Form Plugin to submit a form via AJAX. One thing I'd like to do, is editing the value of a (hidden) input field, right before the form is sent (or alternatively adding additional POST data in some way before send). I defined a hidden input field (without a value) in the form and I tried to change the value attribute with jQuery in the beforeSend section of the ajaxForm Options. Unfortunately this doesn't work, the field still stays empty. This sort of thing comes up a lot so I've added a new callback function called 'beforeSerialize'. You can use it like this: var ajaxFormOptions = { beforeSerialize: function($form, opts) { $(#hidden_input).attr(value,test); } }; $('#form').ajaxForm(ajaxFormOptions); The updated plugin is available here: http://jqueryjs.googlecode.com/svn/trunk/plugins/form/jquery.form.js
[jQuery] Re: Form Plugin: editing/adding data before send
Ok, it seems I still need help here. var ajaxFormOptions = { beforeSerialize: function(){ $(#hidden_input).attr(value,test); } }; $('#form').ajaxForm(ajaxFormOptions); works fine now :). However, in reality, I don't want to set the value of the input field to an arbitrary value, I actually make another ajax request to receive that value, but for some reason the input field stays empty this way: $(document).ready(function() { var ajaxFormOptions = { beforeSerialize: function(){ $.ajax({ method: get, url: ajax.php, data: , success: function(html){ $(#hidden_input).attr(value,html); } }); }, }; $('#form').ajaxForm(ajaxFormOptions); }); (Note: alerting the html var inside the success function of $.ajax displays the correct value, but the hidden_input field value stays empty on the server.) -- View this message in context: http://www.nabble.com/Form-Plugin%3A-editing-adding-data-before-send-tp20359407s27240p20367050.html Sent from the jQuery General Discussion mailing list archive at Nabble.com.
[jQuery] Re: Form Plugin: editing/adding data before send
but for some reason the input field stays empty this way: Yeah, that reason is known as asynchronous programming. When you make that ajax call you will get a response some time in the future, not immediately.
[jQuery] Re: Form Plugin: editing/adding data before send
malsup wrote: but for some reason the input field stays empty this way: Yeah, that reason is known as asynchronous programming. When you make that ajax call you will get a response some time in the future, not immediately. Ooooh, right, I didn't think of that (I am fairly new to making stuff with ajax). I guess there is no way to make the ajaxForm function wait for this particular response? But I guess this is a different topic now.. -- View this message in context: http://www.nabble.com/Form-Plugin%3A-editing-adding-data-before-send-tp20359407s27240p20367347.html Sent from the jQuery General Discussion mailing list archive at Nabble.com.
[jQuery] Re: Form Plugin: editing/adding data before send
malsup wrote: This sort of thing comes up a lot so I've added a new callback function called 'beforeSerialize'. You can use it like this: var ajaxFormOptions = { beforeSerialize: function($form, opts) { $(#hidden_input).attr(value,test); } }; $('#form').ajaxForm(ajaxFormOptions); The updated plugin is available here: http://jqueryjs.googlecode.com/svn/trunk/plugins/form/jquery.form.js Sounds good, thank you very much :) -- View this message in context: http://www.nabble.com/Form-Plugin%3A-editing-adding-data-before-send-tp20359407s27240p20366636.html Sent from the jQuery General Discussion mailing list archive at Nabble.com.
[jQuery] Re: Form Plugin: editing/adding data before send
Ooooh, right, I didn't think of that (I am fairly new to making stuff with ajax). I guess there is no way to make the ajaxForm function wait for this particular response? But I guess this is a different topic now.. The way to do it is to return false from the beforeSerialize function; that will prevent the form from being submitted. Then in your ajax success handler you submit the form manually via ajaxSubmit. Mike