+ Reply to Thread
Results 1 to 7 of 7

Thread: How to put a php variable into javascript

  1. #1
    moneynetworkgroup39's Avatar
    moneynetworkgroup39 is offline x10Hosting Member moneynetworkgroup39 is an unknown quantity at this point
    Join Date
    Aug 2010
    Location
    In my dreams
    Posts
    90

    How to put a php variable into javascript

    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 Code:
    <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.

  2. #2
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: How to put a php variable into javascript

    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 by lemon-tree; 01-25-2011 at 01:34 PM.

  3. #3
    moneynetworkgroup39's Avatar
    moneynetworkgroup39 is offline x10Hosting Member moneynetworkgroup39 is an unknown quantity at this point
    Join Date
    Aug 2010
    Location
    In my dreams
    Posts
    90

    Re: How to put a php variable into javascript

    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 Code:
    myChart.setDataURL("<?php require_once '4.php'$fileaaa=xmltempname::getSettings(); ?>");
    I have checked my source its appearing something like that
    PHP Code:
    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?

  4. #4
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: How to put a php variable into javascript

    "/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 Code:
    $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 by lemon-tree; 01-26-2011 at 05:58 AM.

  5. #5
    moneynetworkgroup39's Avatar
    moneynetworkgroup39 is offline x10Hosting Member moneynetworkgroup39 is an unknown quantity at this point
    Join Date
    Aug 2010
    Location
    In my dreams
    Posts
    90

    Re: How to put a php variable into javascript

    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.

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: How to put a php variable into javascript

    Quote Originally Posted by moneynetworkgroup39 View Post
    PHP Code:
    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.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  7. #7
    moneynetworkgroup39's Avatar
    moneynetworkgroup39 is offline x10Hosting Member moneynetworkgroup39 is an unknown quantity at this point
    Join Date
    Aug 2010
    Location
    In my dreams
    Posts
    90

    Re: How to put a php variable into javascript

    Oops sorry for the mistake seriously I am working on it.

+ Reply to Thread

Similar Threads

  1. PHP Variable in URL
    By webdev2009 in forum Programming Help
    Replies: 7
    Last Post: 05-28-2009, 06:30 PM
  2. Send multiline PHP variable to javascript
    By last2kn0 in forum Programming Help
    Replies: 3
    Last Post: 07-07-2008, 04:33 PM
  3. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM
  4. PHP If variable is set to
    By amr1991 in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 05-10-2007, 07:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers