I need to enable user to click on a link to show a lightbox which has a form, submit the form and redirect user to a new page.
To implement that, I define two javascript functions, lightboxform function to show the lightbox and subform function to submit the form. The problem is that the second javascript function (subform) sends the request to back-end but rather than redirecting to index page append its parameters to the origin address.
Lets say I am on following address:
www.example.com/show?id=2
I click on the link to show the lightbox, after submitting the form located in lightbox, the address of the page get changed to the following address:
www.example.com/show?p=3444&q=4555 << id parameter is replaced by p,q which are the parameters that I am trying to submit to backend
Long story short: I need to enable users to click on a link, a lightbox be shown, user submit the form in the lightbox to back-end and be redirected to the index page.
Code
//function to show lightbox
function lightboxform(){
document.getElementById("lightbox").style.display = "Block";
document.getElementById("cover").style.display = "Block";
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById("lightbox").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("get","../../lightbox/myform",false);
xmlhttp.send();
return false;
}
//function to submit the lightbox form to backend and redirect user to new address
function subform(){
p = $('#p').val();
q = $('#q').val();
document.getElementById("lightbox").style.display = "Block";
document.getElementById("cover").style.display = "Block";
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById("lightbox").style.display = "none";
document.getElementById("cover").style.display = "none";
window.location = ("http://localhost:8080/index.action");
}
}
xmlhttp.open("get","../../lightbox/processForm?p="+p+"&q="+q,false);
xmlhttp.send();
}
lightbox's form
<form id="form393" onsubmit="return subform()">
<input id="p" name="p" type="text"/>
<input id="q" name="q" type="text"/>
<input type="submit" value="submit" />
</form>