我有这个javascript代码
<Script>
function getGroupId(){
var group=document.getElementById("selectedOptions").value;
var groupFirstLetter=group.substring(2);
}
</Script>
我需要将 groupFirstLetter
传递给一个 PHP 函数,该函数将计算该组中有多少用户并将值返回给 javascript 函数。
有什么想法吗?
谢谢,
最佳答案
您可以为此使用 jQuery 来发布 AJAX 请求。请参阅从 jQuery 文档中取出的示例:
$.ajax({
type: "POST",
url: "some.php",
data: {groupFirstLetter:groupFirstLetter},
complete: function(data){
//data contains the response from the php file.
//u can pass it here to the javascript function
}
});
该变量将作为 $_POST['groupFirstLetter']
在您的 PHP 代码中可用。
您可以随意操作它,然后再次向浏览器echo
响应,它将在data
变量中可用。
引用资料:
关于php - 将javascript变量传递给PHP函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10084689/