-2

I want to fetch the list of transactions through jQuery/AJAX so it will update on any page I have that list on, when I update the content of this file I want to use. But I don't want to have the SQL queries in the file that I get through jQuery/AJAX since I use different queries for different pages on my website.

  • The file transactions.php contains all the SQL queries that I need. In that file, I also have this line: <div id="transactions"></div>.
  • The file fetch-transactions.php contains this list I want to show on the page. In it I have 2 globals (you know, global $thevariable) for 2 required variables ($count_transactions (to count the transactions) and $get_transactions (to loop the transactions into the list)) that can be found in transactions.php.
  • The javascripts.js contains all the needed JavaScript codes I have for my website, including this line: $.get(folder_name + 'ajax/fetch-transactions.php', function(s) { $('div#transactions').html(s); });

As you maybe already have figure out, s contains the content of fetch-transactions.php.

The thing is that none of the variables I have set globally in fetch-transactions.php have any data in them. The are empty! Why is that? Is it because they are not globally set? I don't want to use define as a variable. I don't think that is even possible for fetching data from the database :P

How can I fix this problem?

Airikr
  • 6,258
  • 15
  • 59
  • 110

1 Answers1

0

It looks like you're trying to read PHP variables in JavaScript. JavaScript is client-side and PHP is server side. The only way you can read the PHP variables into JavaScript is by outputting them within your fetch-transations.php file in some kind of format JavaScript can read.

There's a pretty decent Stack Overflow resource for this which you should read.

Community
  • 1
  • 1
d3v_1
  • 579
  • 3
  • 12
  • Thanks for this answer. I didn't think about the difference between PHP and JavaScript. I was too eager to make it work. I'll accept it when I can :) – Airikr Jun 27 '16 at 18:40
  • 1
    No problem, it would be quite dangerous to let the JavaScript have access to the PHP directly so the separation is quite helpful in this case. – d3v_1 Jun 27 '16 at 18:43