簡體   English   中英

從 Ajax 和 PHP 在 Footable 中傳遞 session 變量

[英]Pass a session variable in Footable from Ajax and PHP

我正在嘗試在 Footable 中使用 session 變量。

I call a jquery function "getjsonfile()" which will look in my PHP file "getjson.php" my session variable.

所以,如果我在我的“getjsonfile()”function 中發出警報,我會毫無問題地得到 session 變量的值。

但是我不能在我的“jsonfile”變量中傳遞這個值,以便在Footable中使用。

我的代碼有什么問題? 我希望我已經做了足夠的細節。 謝謝 !!!

/*=== My getjson.php File ===*/
<?php
// $_SESSION file_content_json can be either  "content.json" OR "admin.content.json"
session_start();
$file_content_json = $_SESSION['file_content_json'];
die(json_encode(array('file_content_json' => $file_content_json)));
?>



/*=== My function ===*/
function getjsonfile() {
      $.ajax({
      url: 'getjson.php',
      dataType: "json",
      cache: false,
            success: function(data) {
            //alert("file="+data.file_content_json);
            return data.file_content_json;
            },
      });               
};


/*=== My main Footable ===*/
jQuery(function($) {
var jsonfile = getjsonfile();
    
    var $modal = $('#editor-modal'),
        $editor = $('#editor'),
        $editorTitle = $('#editor-title'),

        ft = FooTable.init('#editing-data', {
            columns: $.get("content/"+jsonfile), /* Here, I need my value (session variable) */
            editing: {
                enabled: true,
                addRow: function() {
                    /*.... following stuff ...*/

我不知道你為什么選擇這條路線來分享信息。 無論如何,您可以使用內置的 fetch() 以異步等待返回數據。

getjson.php

<?php
    
    session_start();
    
    $_SESSION['file_content_json'] = "Your session content";
    
    header("Content-Type: application/json");
    exit(json_encode([
        'file_content_json' => $_SESSION['file_content_json']
    ]));

JS文件

async function getjsonfile(){
    const response = await fetch('getjson.php');
    return response.json();
}

// test
(async function(){
    var content = await getjsonfile();
    console.log(content.file_content_json);
})();

// in your case (use async function)
jQuery(async function($) {
    var jsonfile = await getjsonfile();
    
    //..
        ft = FooTable.init('#editing-data', {
            columns: jsonfile.file_content_json,
            editing: {
                enabled: true,
                addRow: function() {
                    //...

Hassan Azzi,我似乎無法讓你的提議完全奏效。

getjsonfile() 運行良好,使用 // test.

但在那之后, jsonfile.file_content_json 仍然被識別。

盡管如此,非常感謝您的回答,這鼓勵我最終找到我的示例的解決方案。

這是我的解決方案:

/*=== My getjson.php File ===*/
<?php
// $_SESSION file_content_json can be either  "content.json" OR "admin.content.json"
session_start();
$file_content_json = $_SESSION['file_content_json'];
die(json_encode(array('file_content_json' => $file_content_json)));
?>

/*=== My function ===*/
function getjsonfile() {
    var result = JSON.parse($.ajax({
        url: 'getjson.php',
        async: false
    }).responseText);
    return result.file_content_json;
}

/*=== My main Footable ===*/
jQuery(function($) {
var jsonfile = getjsonfile();
        //..
        ft = FooTable.init('#editing-data', {
            columns: $.get("content/"+jsonfile),
            editing: {
                enabled: true,
                addRow: function() {
                    //... 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM