简体   繁体   English

从mysql到javascript变量

[英]from mysql to javascript variable

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. 我想从我的mysql数据库中获取数据,然后放入我的javascript变量中。

These two var items in my .js file named test.js and this file is called into my .html file. 我的.js文件中名为test.js这两个var项目,此文件称为我的.html文件。

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 : 在一个名为call.php.php文件中,我使用此方法(PDO)从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: 我想尝试在.js文件test.js实现类似的test.js -这显然行不通,但希望能为我要实现的目标提供一些test.js

var tag_name = '<?php echo $tag_name ?>';
var client_id = '<?php echo $client_id ?>';

Can I do this using an Ajax method? 我可以使用Ajax方法执行此操作吗? In my research I read that I need to use JSON ? 在研究中,我读到我需要使用JSON If the question has been asked before, please direct me to a post that is not just someone dumping 50+ lines of code. 如果以前曾问过这个问题,请引导我到一个帖子中,不仅仅是发表50行以上代码的人。

Here you go: 干得好:

The javascript part: JavaScript部分:

$.ajax({
   url: 'call.php',
   dataType: 'json'
}).done(
   function(data){
     var tag_name = data[0];
     var client_id = data[1];
   }
);

The call.php file: call.php文件:

$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'];
}
echo json_encode(array($tag_name,$client_id));

create a php script that will generate your js and instead of linking your script tag to the .js file link it to the php script that generates the js 创建一个将生成您的js的php脚本,而不是将您的script标记链接到.js文件,而是将其链接到生成js的php脚本

PHP 的PHP

<?php
//mysql stuff up here
header('Content-Type: text/javascript');
?>
//some js here 
var tag_name = '<?php echo $tag_name ?>';
var client_id = '<?php echo $client_id ?>';
// some more js here

HTML 的HTML

<script type="text/javascript" src="http://example.com/test.js.php"></script>

You can echo out your php variables as JSON and then consume them, but if you're just trying to set your js variables with data from php you might want to simply pass the variables in through a function call. 您可以将php变量作为JSON回显,然后使用它们,但是,如果您只想使用来自php的数据设置js变量,则可能需要简单地通过函数调用传递变量。 Adding something like this to your php script to activate the Javascript you're trying to use for the page. 在您的php脚本中添加类似的内容,以激活您要用于该页面的Javascript。

<script type="text/javascript">
    setVariables('<?php echo $tag_name ?>', '<?php echo $client_id ?>');
</script>

and then creating that function in your .js file to utilize those values and do what you need. 然后在.js文件中创建该函数以利用这些值并执行所需的操作。

function setVariables(name, id){
    var tag_name = name
    var client_id = id
    //Operations that need these values.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM