简体   繁体   中英

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. I need to access this table name is a javascript file demo.js. Any ideas would be great.

PHP Code Snip-it:

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

js file code 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.

In your php code:

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

Now you have this variable anywhere in your JS

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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