How to put a php variable into javascript

Messages
92
Reaction score
1
Points
8
I know if misson found this thread his going to take a hand with me :D. (misson please don't mind just kidding). Here come to the point as I told early I am working on chart script I have completed it almost but got a new problem. I am using a chart script like below
PHP:
<script type="text/javascript">
        var myChart = new FusionCharts("../chart/chart.swf", "myChartId", "900", "300", "0", "0");
        myChart.setDataURL("results.xml");
        myChart.render("chartdiv");
    </script>
But in here where results.xml I want to use a php variable. I declared the variable as tempname for no conflict of data. But I am unable to use it in javascript. But I have to call the javascript to avoid cross browser problem. And I am 0 in javascript even I do not like ,uch javascript, ajax and jquery frankly speaking because I do not know that's why. So can any one tell me how can I figure this out?

Thanks,
P.S. misson if you are reading this thread do not get rude on me I was just kidding. And thanks for your help on my previous thread those are really helpful for me.:redface:
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
A code snippet of what's actually not working would be a bit more helpful. Going from the explanation, are you trying to get the PHP variable like so:
Code:
 myChart.setDataURL("$tempname");
If so, that's not going to work as it is being sent literally as $tempname and therefore Javascript is going looking for a file with that name. Javascript has no awareness of the PHP variable scope and regards the page a straight HTML and Javascript. To get PHP to insert the value of the variable there you would have to echo the value and the value must be set prior to this being called:
Code:
myChart.setDataURL("<?php echo $tempname; ?>");
This still seems like a bit of a peculiar way of specifying the desired xml document though.
 
Last edited:
Messages
92
Reaction score
1
Points
8
yes lemon-tree you are the genius. I tried by first option. But second option worked. But I got another issue. I used something like this.
PHP:
myChart.setDataURL("<?php require_once '4.php'; $fileaaa=xmltempname::getSettings(); ?>");
I have checked my source its appearing something like that
PHP:
myChart.setDataURL("/home/JEET/public_html/fedorahelp.tk/chart/StockAdviceCharts_Wed, 26 Jan 2011 12:18:18 +0530_gLNNBv.xml
");
I also tried to enter the exact generated url in my browser. Xml generating correctly and it's opening in my browser. But in chart it's not looks like xml is using. Why that so can you tell me?
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
"/home/JEET/public_html/fedorahelp.tk/chart/StockAdviceCharts_Wed" is the full file path that the PHP and the server has for the file, whereas the web root directory starts at "/home/JEET/public_html", so you need to only return the portion of the string after that point. Depending upon how you are actually getting this file reference there may be a better way of doing it, but otherwise a simple string replace will do the trick:
PHP:
$longUrl = "/home/JEET/public_html/fedorahelp.tk/chart/StockAdviceCharts_Wed" // Your fetched file path
$shortUrl = str_replace($_SERVER['DOCUMENT_ROOT'], "", $longUrl); //Document root should return /home/JEET/public_html/
//$shortUrl is now "fedorahelp.tk/chart/StockAdviceCharts_Wed"
 
Last edited:
Messages
92
Reaction score
1
Points
8
ok I figured it out I't was happening for the blanks I removed it and it works fine. :D thanks lemon-tree for the suggestion it worked excellent.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
PHP:
myChart.setDataURL("<?php require_once '4.php'; $fileaaa=xmltempname::getSettings(); ?>");
Note that you shouldn't put a require (or any statement that might generate an error) in the middle of an embedded statement (in this case, the JS myChart.setDataURL). If there's an error, the result will be a ill-formed document, which will cause all sorts of parse problems and quite possibly corrupt users' browsers, turning them into a slavering army of the undead, intent on converting our graphics cards into a distribute system to descramble cable porn channels.
 
Top