tags:

views:

268

answers:

3

Using jQuery's 'submit' - is there a way to pass additional parameters to a form? I am NOT looking to do this with Ajax - this is normal, refresh-typical form submission.

        $('#submit').click(function () {
            $('#event').submit(function () {
                data: { 
                form['attendees'] = $('#attendance').sortable('toArray').toString();
            });
        });
+1  A: 

In your case it should suffice to just add another hidden field to your form dynamically.

var input = $("<input>").attr("type", "hidden").value("Bla");
$('#form').append($(input));
Daff
The data I need to submit is not form-capable. It has to be captured and handled on the server side.
Stacey
What do you mean by "not form capable"?
Vincent Ramdhanie
Sorry - this didn't work at all. Even with just forced data, it doesn't inject it into the form.
Stacey
It cannot be placed in a form field.
Stacey
If its a string it can be put in a form field
PetersenDidIt
Alright, well... this technique didn't work. it doesn't inject the form element into the form. Putting it in a hidden form field might work, but the code given doesn't.
Stacey
A: 

You don't need to bind the submit event on the click of the submit button just bind the submit event and it will capture the submit event no mater how it gets triggered.

Think what you are wanting is to submit the sortable like you would via ajax. Try doing something like this:

var form = $('#event').submit(function () {
    $.each($('#attendance').sortable('toArray'),function(i, val){
        $("<input>").attr({
            'type':'hidden',
            'name':'attendace['+i+']'
        }).value(val).appendTo(form);
    });
});
PetersenDidIt
still not showing on the server side formcollection... is there somethign special I have to do to get a hidden field to show up on a server? It is going through C#/ASP.NET MVC using a FormCollection object.
Stacey
Updated my answer, think I undertand what you are trying to do.
PetersenDidIt
Are you adding a `name` attribute to the generated hidden form input? As per http://stackoverflow.com/questions/504681/formcollection-empty-on-form-post-in-asp-net-mvc it looks like FormCollection objects require all the `<input>` elements to have names.
npdoty
Yes, I am trying to push a sortable into the formcollection as a string of comma separated values. I've tried your update and it still doesn't post. I'm not sure what I am doing wrong... I appreciate the help.
Stacey
Sounds like the problem isn't on the client side but the server side.
PetersenDidIt
Hrnm. It does sound that way. Here is my method signature. public ActionResult Design(EventViewModel model, FormCollection collection)
Stacey
Would there be a way to use $.post? I'm not aversed to it, but how do I get the original form data into it?
Stacey
You can serialize the form $("form").serialize();
PetersenDidIt
Sigh. Still no luck.
Stacey
So with .serialize, what kind of data on the server type do I expect?
Stacey
+2  A: 

This one did it for me:

var input = $("<input>").attr("type", "hidden").attr("name", "mydata").val("bla");
$('#form1').append($(input));

is based on the Daff answer, but added the NAME attribute to let it show in the form collection and changed VALUE to VAL Also checked the ID of the FORM (form1 in my case)

used the firefox firebug to check wether the element was inserted.

hidden elements do get posted back in the form collection, only read-only fields are discarded.

Michel

Michel
How do I sing 'you are my sunshine' in binary? Thank you so much. This solves so many things.
Stacey
Well I sure forgot the name attribute...
Daff