1

Currently I'm working with ASP.Net and a nested form. My problem is, that ASP.Net only allows one form per page, but I need a kind of sub-form to redirect to another page.

My idea was to create a submit using JavaScript, but I don't get how to set the content of the post, which will be send.

For example I've tried to use the following code:

<div>
    <input type="hidden" value="ABCDE" />
    <input title="Send Form" onclick="this.form.method = 'post'; this.form.action = 'https://externpage/url'; this.form.submit();" type="submit" />
</div>

My problem is, that the content of the request should only be the hidden fields containing ABCDE and not all fields on the page. How can i achieve this using JavaScript and HTML?

Thanks a lot!

10
  • I think in ASP.NET WEbForms you can get tag by Id in codebehind Commented May 3, 2016 at 9:22
  • I redirect to another extern page. They need the hidden value in the content of the request. But I don't know how they are working later with this. Commented May 3, 2016 at 9:23
  • 1
    @UthmanRahimi I think we are talking about two different things. I don't need to access any control on the server side. Commented May 3, 2016 at 9:27
  • 1
    Are these piece of codes actually inside a form tag? Commented May 3, 2016 at 9:28
  • 1
    Have you tried to set the other fields to disabled with the disabled html attribute? Commented May 3, 2016 at 9:37

1 Answer 1

2

Use FormData() to do the encoding.

sendForm=(e)=>{
    let data = new FormData();
    let hidden = document.querySelector('button').parentNode.firstChild.value;
    data.append('hidden',hidden);
    let xhr = new XMLHttpRequest();
    xhr.onreadystatechange=()=>(xhr.readyState == 4) ? console.log(xhr.response) : null;
    xhr.open('POST','http://yoururl.com');
    xhr.send(data);
}
document.addEventListener('DOMContentLoaded',()=>{
    document.querySelector('button').addEventListener('click',sendForm);
});
Sign up to request clarification or add additional context in comments.

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.