230

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();
    });
});
3

6 Answers 6

404

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's 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 whether the element was inserted.

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

Michel

Sign up to request clarification or add additional context in comments.

6 Comments

How do I sing 'you are my sunshine' in binary? Thank you so much. This solves so many things.
You can slightly improve readability: var input = $("<input>", { type: "hidden", name: "mydata", value: "bla" }); $('#form1').append($(input));
$("<input>").attr("type", "hidden").attr("name", "mydata").val("bla").appendTo('#form1'); is another way
I liked a combo of: $("<input>", { type: "hidden", name: "mydata", value: "bla" }).appendTo("#form1");
Isn't there a limit on data size? I sent some big object using JSON.stringify(obj) and this method and it appeared to be truncated.
|
28

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

var input = $("<input>").attr("type", "hidden").val("Bla");
$('#form').append($(input));

7 Comments

The data I need to submit is not form-capable. It has to be captured and handled on the server side.
What do you mean by "not form capable"?
Sorry - this didn't work at all. Even with just forced data, it doesn't inject it into the form.
It cannot be placed in a form field.
If its a string it can be put in a form field
|
22

You can even use this one. worked well for me

$("#registerform").attr("action", "register.php?btnsubmit=Save") 
$('#registerform').submit();

this will submit btnsubmit =Save as GET value to register.php form.

3 Comments

This seems like a more elegant solution. Just remember to use encodeURIComponent() for the parameter value depending on the type of data.
This one is actually better when allow user submit form multiple times. The form won't get more than one hidden fields with different values.
what about POST method
12

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, value){
        $("<input>").attr({
            'type':'hidden',
            'name':'attendace['+i+']'
        }).val(value).appendTo(form);
    });
});

9 Comments

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.
Updated my answer, think I undertand what you are trying to do.
Are you adding a name attribute to the generated hidden form input? As per stackoverflow.com/questions/504681/… it looks like FormCollection objects require all the <input> elements to have names.
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.
Sounds like the problem isn't on the client side but the server side.
|
11

You could write a jQuery function which allowed you to add hidden fields to a form:

// This must be applied to a form (or an object inside a form).
jQuery.fn.addHidden = function (name, value) {
    return this.each(function () {
        var input = $("<input>").attr("type", "hidden").attr("name", name).val(value);
        $(this).append($(input));
    });
};

And then add the hidden field before you submit:

var frm = $("#form").addHidden('SaveAndReturn', 'Save and Return')
                    .submit();

2 Comments

See stackoverflow.com/questions/2601223/… for more advanced version of addHidden
I like the simplicity of this solution. It does not handle many cases but it nicely handle the case it does.
5

Similar answer, but I just wanted to make it available for an easy/quick test.

var input = $("<input>")
               .attr("name", "mydata").val("go Rafa!");

$('#easy_test').append(input);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>



<form id="easy_test">
  
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.