user1224860
user1224860

Reputation: 11

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. even I copied .js code in that PHP file but in the main file(index.php) I am facing conflict of the js file . Thats why I cant copied js file into that php file . Please provide me the soution. below that I am copied my code of PHP as well as js file.

In PHP code

$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

Code in the number.js file

var leads=0;

I want the var_leads=$total_lead (the value come from the php file).

How it is possible?

Upvotes: 0

Views: 906

Answers (5)

Rahul Tapali
Rahul Tapali

Reputation: 10137

You can use this:

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

Upvotes: 0

Jeff Wooden
Jeff Wooden

Reputation: 5479

You could also run your PHP inline with the JavaScript. 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>

Upvotes: 0

thescientist
thescientist

Reputation: 2948

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.

If this script also loads a page, then you need to echo out the value in the context of actual tags.

Upvotes: 0

Marc B
Marc B

Reputation: 360572

.js files will be default NOT be executed as PHP scripts on a server, unless you tell the webserver to do so. That means you cannot embed PHP code into a .js file and have it fill things in for you.

Unless you do want to modify your server to force PHP handling of .js files, you'd be better off doing something like this:

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.

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.

Upvotes: 1

HJW
HJW

Reputation: 23443

You can look here for appropriate PHP-JSON libraries to do this job:

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

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

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

Upvotes: 0

Related Questions