简体   繁体   English

如何将json值php传递给js

[英]How to pass json value php to js

In my one of php file I am running one query whatever query result is display that value I want to put in the number.js file. 在我的一个php文件中,无论查询结果显示了我要放入number.js文件中的值,我都在运行一个查询。 even I copied .js code in that PHP file but in the main file(index.php) I am facing conflict of the js file . 即使我在该PHP文件中复制了.js代码,但在主文件(index.php)中,我也面临着js文件的冲突。 Thats why I cant copied js file into that php file . 那就是为什么我不能将js文件复制到该php文件中的原因。 Please provide me the soution. 请提供我的意见。 below that I am copied my code of PHP as well as js file. 在下面,我复制了我的PHP代码以及js文件。

In PHP code 用PHP代码

$query_string = "SELECT COUNT(Email) AS total FROM Contact INNER JOIN CompanyBranch ON Contact.CompanyBranchID = CompanyBranch.CompanyBranchID INNER JOIN Company ON Company.CompanyID = CompanyBranch.CompanyID INNER JOIN CompanyIndustry ON Company.CompanyID =  CompanyIndustry.CompanyID INNER JOIN IndustrySubindustry ON CompanyIndustry.IndustrySubindustryID = IndustrySubindustry.IndustrySubindustryID WHERE CompanyBranch.GlobalRegionID = '$global_region' AND IndustrySubindustry.IndustryID = '$industry' AND IndustrySubindustry.SubindustryID = '$sub_industry'";
$query_string = strtolower($query_string);
$result_data = mysql_query($query_string) or die();

//$tmp = mysql_fetch_array($result_data);
$row = mysql_fetch_assoc($result_data);
$total_lead = $row['total'];
echo json_encode($total_lead);

whatever value get in the $total_lead variable that I want to redirect into the number.js file 我想重定向到number.js文件的$total_lead变量中获取任何值

Code in the number.js file number.js文件中的代码

var leads=0;

I want the var_leads=$total_lead (the value come from the php file). 我想要var_leads=$total_lead (该值来自php文件)。

How it is possible? 怎么可能?

.js files will be default NOT be executed as PHP scripts on a server, unless you tell the webserver to do so. 默认情况下,.js文件不会在服务器上作为PHP脚本执行,除非您告知Web服务器这样做。 That means you cannot embed PHP code into a .js file and have it fill things in for you. 这意味着您无法将PHP代码嵌入到.js文件中,而无法为您填充内容。

Unless you do want to modify your server to force PHP handling of .js files, you'd be better off doing something like this: 除非您确实想修改服务器以强制对.js文件进行PHP处理,否则最好这样做:

yourfile.php: yourfile.php:

<?php
   ... do query stuff here ...
?>
<html>
<head>
   <script type="text/javascript">var leads = <?php echo json_encode($total_leader) ?>;</script>
   <script type="text/javascript" src="number.js"></script>
</head>

That'll set the leads variable for you with the query results, then load the rest of the number.js script which then (supposedly) uses that variable. 这将为您设置带查询结果的Leads变量,然后加载number.js脚本的其余部分,然后(假定)使用该变量。

The alternative is having a piece of JS code that performs an AJAX call back to your server to fetch the number dynamically at page load time. 另一种方法是使用一段JS代码执行AJAX回调到您的服务器,以便在页面加载时动态获取该数字。

You can look here for appropriate PHP-JSON libraries to do this job: 您可以在这里找到合适的PHP-JSON库来完成此工作:

http://www.php.net/releases/5_2_0.php http://www.php.net/releases/5_2_0.php

http://pecl.php.net/package/json http://pecl.php.net/package/json

JSON page: http://www.json.org/ JSON页面: http//www.json.org/

I don't know the full context of your application, but if you are making an AJAX request to this script, you need to use a callback or onreadystate change, depending on wether the call is asynchronous. 我不知道您的应用程序的完整上下文,但是如果您要对此脚本进行AJAX请求,则需要使用回调或onreadystate更改,具体取决于调用是否异步。

If this script also loads a page, then you need to echo out the value in the context of actual tags. 如果此脚本还加载页面,则需要在实际标签的上下文中回显该值。

You could also run your PHP inline with the JavaScript. 您也可以使用JavaScript内联运行PHP。 Take a look at this quick example for instance: 例如,看一下这个快速示例:

<?php
$query_string = "Your select statement...";
$query_string = strtolower($query_string);
$result_data = mysql_query($query_string) or die();

//$tmp = mysql_fetch_array($result_data);
$row = mysql_fetch_assoc($result_data);
$total_lead = $row['total'];
?>
<script type="text/javascript">
var leads = <?=$total_leads?>; //as integer
var leads = '<?=$total_leads?>'; //as string
//other js stuff
</script>

You can use this: 您可以使用此:

        var obj = <?php echo json_encode(your_json_object) ?>
        var jsonObject = JSON.parse(obj) 

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

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