gpt4 book ai didi

php - 发送全局变量

转载 作者:行者123 更新时间:2023-11-29 05:40:37 25 4
gpt4 key购买 nike

我有一段 Javascript 将用户选择的信息转发到外部 PHP 文件,并返回信息。在下面的代码中,您可以看到它通过 POST 将 {'report' : report} 发送到该文件。这很好用。

本质上,我需要添加另一个要发送的变量。它被称为“id”,但它在另一个函数中。有没有办法使该变量成为全局变量,然后将其合并,以便将其发送到我的代码片段中? (全局变量何时会被清除?)我也可以通过“url”属性发送它,并在我的 PHP 中使用 GET...只是不确定如何实现。

$('#adminSelectReport a').live("click", function () {
//Get id from clicked link:
var report = $(this).attr('id');

$.ajax({
type: 'POST',
url: 'getReportInfo.php',
data: {
'report': report
},
success: function (msg) {
//everything echoed in your PHP-File will be in the 'msg' variable:
$('#adminReportInfo').html(msg);
$('#adminReportInfo').fadeIn(400);
}
});
});

更新:这是将“id”发送到另一个页面以获取信息的另一个片段。但是,我需要保留此 ID,并在我的原始代码中使用它。

$('#adminSelectCompany a').click(function() {
//Get id from clicked link:
var id = $(this).attr('id');

$.ajax({
type: 'POST',
url: 'getReports.php',
data: {'id': id},
success: function(msg){
//everything echoed in your PHP-File will be in the 'msg' variable:
$('#adminSelectReport').html(msg);
$('#adminSelectReport').fadeIn(400);
$('#adminReportInfo').fadeOut(300);

}
});
});

最佳答案

这听起来像是他们通过一个链接选择了一家公司,然后他们通过另一个链接选择了一份报告,您需要记住选择了哪个公司。

为了避免全局变量,我可能只是将一个类添加到选定的公司链接,然后通过选定的类获取该元素,并获取其 ID。如果需要,您也可以使用该类进行样式设置。

var companies = $('#adminSelectCompany a');

companies.click(function () {

// remove class from previously selected, and add to new one
companies.filter('.selected').removeClass('selected');
$(this).addClass('selected');

$.ajax({
type: 'POST',
url: 'getReports.php',
data: {
'id': this.id
},
success: function (msg) {
//everything echoed in your PHP-File will be in the 'msg' variable:
$('#adminSelectReport').html(msg)
.fadeIn(400);
$('#adminReportInfo').fadeOut(300);

}
});
});
$('#adminSelectReport a').live("click", function () {

$.ajax({
type: 'POST',
url: 'getReportInfo.php',
data: {
'report': this.id,
'company': $('.selected')[0].id
},
success: function (msg) {
//everything echoed in your PHP-File will be in the 'msg' variable:
$('#adminReportInfo').html(msg);
$('#adminReportInfo').fadeIn(400);
}
});
});

关于php - 发送全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7052876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com