1
if(!isset($_GET['t'])){$table = 'notes';}
else{$table = $_GET['t'];}

$cats = $table . '_cats';

include 'test.php';

test.php

test(); // works fine

if(isset($_POST['fn'])){
    $_POST['fn']();
}

function test(){
    global $cats, $table;
    echo $cats;
    echo $table;
}

js

$(document).on('click', '.atitle', function(){
    $.post('test.php', {fn: 'test'}, function(data){
        console.log(data); // empty
    });
});

I'm expecting$cats and $table written in console

Any help?

5
  • are you confirmed to received test in $_POST['fn'] ? please check first Commented Mar 28, 2020 at 5:00
  • @prasanth - yes because echo 'lorem' inside fn test - works Commented Mar 28, 2020 at 5:03
  • What is the file name where you written include 'test.php'; ? Commented Mar 28, 2020 at 5:07
  • try echo "$cats , $table"; exit; Commented Mar 28, 2020 at 5:07
  • @RishiRaut - a_notes.php Commented Mar 28, 2020 at 5:09

1 Answer 1

2

As your first file name is a_notes.php then call a_notes.php from ajax. You are defining $cats, $table in a_notes.php not in test.php.

$(document).on('click', '.atitle', function(){
    $.post('a_notes.php', {fn: 'test'}, function(data){
        console.log(data);
    },'json');// set dataType as json
});

Also in test.php, As you are calling a function test() then echo returned value of function.

echo test();

if(isset($_POST['fn'])){
    $_POST['fn']();
}

function test(){
    global $cats, $table;
    return json_encode(["cats"=>$cats,"table"=>$table]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

but test.php is included in a_notes.php. I tried to define cats and table inside test.php - the same result. a_notes.php is the main file.
@qadenza: Call $.post('a_notes.php'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.