【问题标题】:getting php session variable in js file在js文件中获取php会话变量
【发布时间】:2015-10-21 19:36:32
【问题描述】:

我有一个带有 php if 语句的 php 文件,它为 mySQL 表名生成一个 php 会话变量。我需要访问的这个表名是一个javascript文件demo.js。任何想法都会很棒。

PHP 代码截图:

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

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();   
}

【问题讨论】:

  • 您的 JS 文件需要作为 PHP 执行,可能只是更改扩展名,然后在 JS 中使用 <?php echo $_SESSION["paycycletable"]; ?> 而不是演示。
  • 由于调用了js文件中的js函数,这个不起作用。
  • 这没有任何意义......
  • 如果文件无法更改为 php 文件,那么据我所知,您唯一的选择仍然是使用 ajax 加载会话值。
  • 如果安全性不高,您可能需要设置$_COOKIE 而不是$_SESSION。然后 JS 可以从 cookie 的后缀中读取。

标签: javascript php mysql editablegrid


【解决方案1】:

将表名保存在 JS 全局空间中。

在您的 php 代码中:

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

现在你的 JS 中的任何地方都有这个变量

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

【讨论】:

    猜你喜欢
    • 2010-09-25
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 2018-05-09
    • 1970-01-01
    相关资源
    最近更新 更多