PHP通过javascript传递多个变量

时间:2015-03-24 03:27:37

标签: javascript php html ajax mysqli

我有以下Javascript函数,其中每个函数都向URL添加一个变量,我想将它们组合起来:

<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();
    }
}

它有问题,因为两个变量都需要出现在url中,否则,我得到一个未被选中的错误。更准确地说,我想结合起来:

      xmlhttp.open("GET","getuser.php?delivery="+str,true);

    xmlhttp.open("GET","getuser.php?duration="+str,true);

提前致谢,非常感谢任何帮助。

更新

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>

更新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' /> &nbsp; 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();
  }
}

1 个答案:

答案 0 :(得分:2)

我这样做:

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
  }
}
如果你封装它,那么

甚至更好,这样就不会暴露变量。