JavaScript post request like a form submit


No. You can’t have the JavaScript post request like a form submit.

What you can have is a form in HTML, then submit it with the JavaScript. (as explained many times on this page).

You can create the HTML yourself, you don’t need JavaScript to write the HTML. That would be silly if people suggested that.

<form id="ninja" action="http://example.com/" method="POST">
  <input id="donaldduck" type="hidden" name="q" value="a">
</form>

Your function would just configure the form the way you want it.

function postToURL(a,b,c){
   document.getElementById("ninja").action     = a;
   document.getElementById("donaldduck").name  = b;
   document.getElementById("donaldduck").value = c;
   document.getElementById("ninja").submit();
}

Then, use it like.

postToURL("http://example.com/","q","a");

But I would just leave out the function and just do.

document.getElementById('donaldduck').value = "a";
document.getElementById("ninja").submit();

Finally, the style decision goes in the ccs file.

#ninja{ 
  display:none;
}

Personally I think forms should be addressed by name but that is not important right now.

javascript submit form with parameters, But when I give a GET method in my HTML form, then the HTML form data is submitted i.e. gotopage.php?data1=one&data2=two is submitting not Javascript action value i.e gotopage.php?parameter1=”+k+”&parameter2=”+m So how to submit the form with Javascript parameter when the method is GET in the HTML form? But when I give a GET method in my HTML form, then the HTML form data is submitted i.e. gotopage.php?data1=one&data2=two is submitting not Javascript action value i.e gotopage.php?parameter1=”+k+”&parameter2=”+m So how to submit the form with Javascript parameter when the method is GET in the HTML form?

document.body.innerHTML += '<form id="dynForm" action="http://example.com/" method="post"><input type="hidden" name="q" value="a"></form>';
document.getElementById("dynForm").submit();

Leave a Comment

Your email address will not be published. Required fields are marked *

Hire a server Expert to resolve the issue Now.

Hire a server Expert to resolve the issue Now.

Resolve this issue in just 5$ from https://serverexpert.io

 

You have Successfully Subscribed!

Open chat