php - 发送全局变量

标签 php javascript mysql html css

我有一段 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/

相关文章:

javascript - 关闭浏览器窗口而不等待ajax响应的最佳实践

javascript - 避免在 ES6 中使用 .bind =>

mysql - 如何在 wordpress 中运行 mysql 查询?

java - 参数化/清理包含数学函数和其他列的 PreparedStatements

python - 迁移: Creating UserProfile in Django/MySQL

php - 使用ajax登录php

php - 从 CSV 到 MySQL 的换行问题

php - 从 XPath 生成 XML

javascript - 如何将用户输入从 javascript 发布到 python?

php - 解析错误 : syntax error, C :\wamp64\www\PHP\New\input. php 中的意外文件结尾第 25 行