I have the following Javascript functions, where each one add a variable to the URL, and I would like to combine them:
<script>
function showDuration(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "Loading Please Wait";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?duration="+str,true);
xmlhttp.send();
}
}
function showDelivery(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "Loading Please Wait";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?delivery="+str,true);
xmlhttp.send();
}
}
Its problematic, because both variable needs to be present in the url, or else, i get an error for the one that's not selected. More precisely, I would like to combine:
xmlhttp.open("GET","getuser.php?delivery="+str,true);
and
xmlhttp.open("GET","getuser.php?duration="+str,true);
Thanks in advance, and any help would be greatly appreciated.
update
var duration = null;
var delivery = null;
function setDuration(_duration) {
duration = _duration;
makeRequest();
}
function setDelivery(_delivery) {
delivery = _delivery;
makeRequest();
}
function makeRequest() {
if (duration != null && delivery != null) {
var url =
"getuser.php?duration=" + encodeURIComponent(duration) +
"&delivery=" + encodeURIComponent(delivery);
// do the ajaxy stuff
}
}
function showDuration(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "Loading Please Wait";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?duration="+str,true);
xmlhttp.send();
}
}
function showDelivery(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "Loading Please Wait";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?delivery="+str,true);
xmlhttp.send();
}
}
</script>
update 2:
<script>
var duration = null;
var delivery = null;
function showDuration(_duration) {
duration = _duration;
makeRequest();
}
function showDelivery(_delivery) {
delivery = _delivery;
makeRequest();
}
function makeRequest() {
if (duration != null && delivery != null) {
var url =
"getuser.php?duration=" + encodeURIComponent(duration) +
"&delivery=" + encodeURIComponent(delivery);
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
document.getElementById("txtHint").innerHTML = "<div class='loading-indication'><img src='ajax-loader.gif' /> Please wait... Loading New Courses...</div>";
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET",url);
xmlhttp.send();
}
}