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
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:
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
JS file