Window.location.href using GET when I need POST

I have the following script, a few lines at the bottom. I have window.location.href

one that posts results from my form, though the address bar. It's very messy and I would like to use POST instead of GET, any ideas anyone?

<script language="javascript">
function OnChangedUsername()
{
  if(document.signup.newuserid.value == "")
  {
    document.signup.btnCheckAvailability.disabled = true;
  }
  else
  {
    document.signup.btnCheckAvailability.disabled = false;
  }
}
function createRequestObject() {
  var ro;
  var browser = navigator.appName;
  if(browser == "Microsoft Internet Explorer"){
    ro = new ActiveXObject("Microsoft.XMLHTTP");
  }else{
    ro = new XMLHttpRequest();
  }
  return ro;
}

var http = createRequestObject();

function sndReq() {
  http.open('get', 'password_check.asp?emailaddress=<%Response.Write(emailaddress)%>&check=<%Response.Write(check)%>&password_check='+document.signup.newuserid.value);
  http.onreadystatechange = handleResponse;
  http.send(null);
}

function handleResponse() {
  if(http.readyState == 4){
    var response = http.responseText;
    var update = new Array();

    if(response.indexOf('|' != -1)) {
      update = response.split('|');
      document.getElementById("username_chk").innerHTML = update[0];


      if(document.getElementById("username_chk").innerHTML == "Ok") {
        window.location.href='detailsupdate.asp?username=<%Response.Write(sUsername)%>&check=<%Response.Write(check)%>&EmailAddress='+document.signup.EmailAddress.value+'&Address='+document.signup.Address.value+'&Address1='+document.signup.Address1.value+'&Address2='+document.signup.Address2.value+'&City='+document.signup.City.value+'&PostalCode='+document.signup.PostalCode.value+'&Country='+document.signup.Country.value+'&WorkPhone='+document.signup.WorkPhone.value+'&HomePhone='+document.signup.HomePhone.value+'&MobilePhone='+document.signup.MobilePhone.value+'&FaxNumber='+document.signup.FaxNumber.value+'&AlternativePhone='+document.signup.AlternativePhone.value+'&OO='+document.signup.OO.checked+'&Workshop='+document.signup.Workshop.checked+'&Raised='+document.signup.Raised.checked+'&Ground='+document.signup.Ground.checked+'&pd='+document.signup.newuserid.value+'&Tram='+document.signup.Tram.checked;
      }
    }
  }
}
</script>

      

0


source to share


2 answers


The only way to make a POST request with a response displayed directly in the browser window is to use a form (which you create with the DOM) and submit it (which you can also do via the DOM).



An alternative is to use XMLHttpRequest (or some other Ajax technique) and render the response using the DOM.

+1


source


There is no need to pass the form data as GET data. You can specify the method as POST.

<form action="script.asp" method="post">
   ...
   <input type="submit" value="submit">
</form>

      



You shouldn't use either window.location.href

. This is very ugly. When you submit the form (click the submit button) it will submit your form data to script.asp.

Another way to submit the form is to submit it via the DOM. document.forms[0].submit();

+1


source







All Articles