Looking for a simple solution here, I have looked around but nothing seems to give me a simple solution. I want to get data FROM my mysql database and then into my javascript variables.
These two var items in my .js
file named test.js
and this file is called into my .html
file.
var tag_name = 'example';
var client_id = '123456789';
In a .php
file called call.php
I use this method (PDO) to get the required data from MySQL
:
$stmt = $db->query('SELECT * FROM data WHERE id=1');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$tag_name = $row['tag_name'];
$client_id = $row['client_id'];
}
I want to try and achieve something along the lines of this in the .js
file test.js
- this obviously wont work but hopefully sheds some light on what I am trying to achieve:
var tag_name = '<?php echo $tag_name ?>';
var client_id = '<?php echo $client_id ?>';
Can I do this using an Ajax
method? In my research I read that I need to use JSON
? If the question has been asked before, please direct me to a post that is not just someone dumping 50+ lines of code.