skip to Main Content

I’m trying to use session variable in Footable.

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

So, If I do an Alert in my "getjsonfile()" function, I get without problem the value of the session variable.

But I can’t pass this value in my "jsonfile" variable for use in Footable.

What is wrong with my code ? I hope I have done enough detail. Thanks !!!

/*=== 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 ...*/

2

Answers


  1. Chosen as BEST ANSWER

    Hassan Azzi, I can't seem to get your proposal to work completely.

    getjsonfile() works well, and the value is displayed correctly with // test.

    But after that, jsonfile.file_content_json remains recognized.

    Nevertheless, many thanks for your answer which encouraged me to finally find a solution to my example.

    Here is my solution:

    /*=== 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() {
                        //... 
    

  2. I’m not sure why did you choose this route to share information. anyway, you could use the built-in fetch() to return the data with async await.

    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 file

    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() {
            //...
          }
        }
      }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search