【问题标题】:How do you call a php function from a javascript function and pass a variable at the same time?如何从 javascript 函数调用 php 函数并同时传递变量?
【发布时间】:2013-08-18 09:05:24
【问题描述】:

现在我有这个:

  var ajax = getRequest();
  ajax.onreadystatechange = function(){
      if(ajax.readyState == 4){
          document.getElementById('results').innerHTML = ajax.responseText;
      }
  }
  ajax.open("GET", "get_schedule.php", true);
  ajax.send(null);
  }

        function getRequest() {
        var req = false;
        try{
            // most browsers
            req = new XMLHttpRequest();
        } catch (e){
            // IE
            try{
                req = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                // try an older version
                try{
                    req = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e){
                    return false;
                }
            }
        }
        return req;
    }

它允许我使用代码 get_schedule.php 调用 php 文件。但是我想将一个变量与 ajax 函数一起传递。我对 Ajax 很陌生,那么我该怎么做呢?

【问题讨论】:

    标签: php javascript jquery ajax function


    【解决方案1】:

    由于您使用的是GET,因此您可以通过将参数附加到 URL 来传递参数:

    ajax.open("GET", "get_schedule.php?paramname=" + encodeURIComponent(paramvalue), true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 2015-06-08
      • 2015-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      相关资源
      最近更新 更多