By using window.location
you are sending the parameters by the URL - which means they are sent by a GET
method and not by POST
.
To send data to the server using POST
method, you'll have to either send it by a form which is defined with method="post"
or, if you don't want to create a form element, by an AJAX XMLHttpRequest
request.
I would suggest, for learning and simplicity, to start with a simple form, such as:
<form action="your-page-after-submittion.html" method="post">
<p>Your full name: <input type="text" name="fullname" /><br />
E-mail: <input type="text" name="email" /></p>
<p>Your message:<br />
<textarea name="message" rows="10" cols="40"></textarea></p>
<p><input type="submit" value="Send the from"></p>
</form>
Or if you want an AJAX request, you can use the following:
//prepare your data values to be sent in the request
var fullname = document.getElementById("fullname").value;
var email = document.getElementById("email").value;
var message = document.getElementById("message").value;
//create an XMLHttpRequest
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//submit the request to your desired page (user will not be redirected, as this is AJAX request
xmlhttp.open("POST","your-page-after-submittion.html",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fullname="+fullname+"&email="+email+"&message="+message);