0

How would I submit the id into the form name to submit a certain form?

function submitComment(id)
{
    alert('comment on song id: '+[id]+'');

    document.postcomment.submit();

}

I want to be able to submit.. postcomment43 or postcomment 42.. whatever the value of ID is joint to postcomment

Tried this:

function submitComment(id)
{
    alert('comment on song id: '+[id]+'');


    var formToSubmit = 'postcomment' + id;

    alert( ''+ formToSubmit +'' );
    document.formToSubmit.submit();


}

creates the formToSubmit name correctly but doesn't fire. how do I properly place the formToSubmit variable in the document.form.submit

It seems that just putting in formToSubmit is going to look for the form with name formToSubmit

2 Answers 2

2

Give your forms an unique ID:

<form id="postcomment42" action="..."></form>
<form id="postcomment43" action="..."></form>
<form id="postcomment44" action="..."></form>

Then use the getElementById function to get the desired form:

function submitComment(id)
{
    var formToSubmit = document.getElementById('postcomment' + id);
    if (formToSubmit != null) formToSubmit.submit();
}

Although I suspect there's something wrong with your design. Why do you have multiple forms? Can't you have a single form to submit a comment like:

<form id="postcomment" action="comment?id=42" method="post"></form>

Could you give a little more details on what the interface looks like and what you are trying to achieve?

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

Comments

0

If your forms only have names, and not ID's, then you could do the following:

// by name
function submitComment(id)
{
    var theForm = 'postcomment' + id;
    document.forms[ theForm ].submit();
}

The are many, many ways to submit a form via JS, but considering your question I think this is the most applicable manner.

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.