1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Pass a php variable to javascript function

Discussion in 'PHP' started by pixelperfectdesigner, Aug 25, 2008.

  1. #1
    Is it possible to pass a php variable to a javascript function? If so, please give an example.

    thanks
     
    pixelperfectdesigner, Aug 25, 2008 IP
  2. Wrighty

    Wrighty Peon

    Messages:
    199
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What you should do is echo it:

    <?
    $x = 'hai!';
    echo "<script>var x  = '" . $x . "';</script>";
    ?>
    Code (php):
    Returns:
    <script>var x = 'hai!';</script>
     
    Wrighty, Aug 25, 2008 IP
  3. pixelperfectdesigner

    pixelperfectdesigner Banned

    Messages:
    495
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, that works if the php codes are in the same file as the javascript.

    Ok what I want to do is to reference a php variable ($filename) in a js function. But..... I want to do that without running the php file, or maybe load it in the background if possible. Hope that make sense.

    If you can help with this problem Im happy to give you a permanent link on my site.

    Thanks
     
    pixelperfectdesigner, Aug 26, 2008 IP
  4. kasapa

    kasapa Peon

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    try this

    in js file
    in your code
     
    kasapa, Aug 26, 2008 IP
  5. Ilyes

    Ilyes Banned

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Kasapa said:
    Yes, this is nice. And he can do it like that:
    And at the end of the php file "echo $var;"

    You will need to convert your current html page to php to get php work on it. If you don't want to rename it to .php extension, check this tip:
    run php script in html page

    Good luck !
     
    Ilyes, Aug 26, 2008 IP
  6. pixelperfectdesigner

    pixelperfectdesigner Banned

    Messages:
    495
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    there's no button..and don't want to add a button on the middle of the page. Ok basically my site is image hosting. The image file name variable is in upload.php and it's called $filename. Because I'm using Flash to upload and upload.php will never be loaded by the browser, it's used by the Flash script ONLY. After the images are uploaded I want to somehow pass the variable $filename to the js script.
     
    pixelperfectdesigner, Aug 26, 2008 IP