简体   繁体   English

在js文件中获取php会话变量

[英]getting php session variable in js file

I have a php file with a php if statement that generates a php session variable for a mySQL table name. 我有一个带有php if语句的php文件,该语句为mySQL表名生成php会话变量。 I need to access this table name is a javascript file demo.js. 我需要访问该表的名称是一个javascript文件demo.js。 Any ideas would be great. 任何想法都很棒。

PHP Code Snip-it: PHP代码片段:

$fcyclestartdate = $_POST['CycleStartDate'];
$fcycleenddate = $_POST['CycleEndDate'];
$_SESSION["paycycletable"] = "PCR_".str_replace("-", "", $fcyclestartdate)."_".str_replace("-", "", $fcycleenddate);

js file code Snip-it: js文件代码Snip-it:

function DatabaseGrid() 
{ 
    this.editableGrid = new EditableGrid("demo", {//Variable table name must go where "demo" is
        enableSort: true,
        // define the number of row visible by page
        pageSize: 50,
        // Once the table is displayed, we update the paginator state
        tableRendered:  function() {  updatePaginator(this); },
        tableLoaded: function() { datagrid.initializeGrid(this); },
        modelChanged: function(rowIndex, columnIndex, oldValue, newValue, row) {
            updateCellValue(this, rowIndex, columnIndex, oldValue, newValue, row);
        }
    });
    this.fetchGrid();   
}

Save the table name in JS global space. 将表名称保存在JS全局空间中。

In your php code: 在您的PHP代码中:

echo "<script> paycycletable='" . $_SESSION["paycycletable"] . "'</script>";

Now you have this variable anywhere in your JS 现在,您可以在JS中的任何位置使用此变量

this.editableGrid = new EditableGrid(paycycletable, {...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM