[英]How do you call a php function from a javascript function and pass a variable at the same time?
Right now I have this: 现在我有这个:
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;
}
which allows me to call a php file with code get_schedule.php. 这允许我用代码get_schedule.php调用一个php文件。 However I would like to pass a variable along with the ajax function. 但是我想传递一个变量和ajax函数。 I am pretty new to Ajax, so how would I do this? 我对Ajax很新,所以我该怎么做?
由于您正在使用GET
,因此您可以通过将参数附加到URL来传递参数:
ajax.open("GET", "get_schedule.php?paramname=" + encodeURIComponent(paramvalue), true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.