Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I submit a form with no form ID and no submit ID, but known hidden value?

the situation I'm struggling with is that there are more forms on the page that looks like this (the hiddenId is different in each form):

<form method="post">
<input type="hidden" name="hiddenId" value="111222">
<input type="submit" value="Proceed">
</form>

<form method="post">
<input type="hidden" name="hiddenId" value="111333">
<input type="submit" value="Proceed">
</form>

How do I submit with javascript (I don't mind using jQuery) a specific form that includes hiddenId of a wished value? Thank you for your help!

like image 966
Sidd Sidd Avatar asked Jan 12 '12 21:01

Sidd Sidd


People also ask

How do you send a hidden value in a form?

The <input type="hidden"> defines a hidden input field. A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted. A hidden field often stores what database record that needs to be updated when the form is submitted.

Can we submit form without form tag?

It is actually possible to "submit" a "form" without form-tags. what you need is an ajax-function, e.g. with jquery, which will get the values by id. But there is no reason not to use a form tho. you should use form tag.

Can two forms have the same ID?

duplicate IDs are illegal in html. what your code is doing is the proper behavior - dealing with the FIRST id found in the document, and ignoring the rest (the dupes). read the docs for getElementById().

What is id in a form?

The id attribute specifies a unique id for an HTML element. The value of the id attribute must be unique within the HTML document. The id attribute is used to point to a specific style declaration in a style sheet. It is also used by JavaScript to access and manipulate the element with the specific id.


1 Answers

Something along these lines should get you started:

var inputs = document.getElementsByTagName("input");
for(var i = 0; i < inputs.length; i++) {
    if(inputs[i].type === "hidden" && inputs[i].value === "111333") {
        inputs[i].form.submit();
    } 
}

If you can use jQuery:

$("input[type='hidden'][value='something']").closest("form").submit();
like image 107
James Allardice Avatar answered Oct 21 '22 17:10

James Allardice