SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    access javascript variable in php

    Hi

    <script type="text/javascript">
    //the callback -- what do we do with the json response?
    function get_delicious_count(info) {
    //get the number of saves
    var num = info[0].total_posts
    //if none, do nothing
    if(!num) return;
    //if some, I add the number to the end of my link, like at the top of every one of my article posts.
    return $('delic').set({
    'text': $('delic').get('text') + ' (' + num + ')',
    'title': num + ' people found this post delicious!'
    });
    }
    </script>

    <!-- GET THE JSON DATA -->
    <script src='http://badges.del.icio.us/feeds/json/url/data?url=http://davidwalsh.name/delicious-url-count-javascript&callback=get_delicious_count'></script>

    I have taken this code from a website. Now I want the num variable value in a php variable. Can anybody tell me how to get it in php?

    Thanks,
    Manju

  2. #2
    SitePoint Evangelist mrwooster's Avatar
    Join Date
    Jan 2006
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't get a javascript variable in PHP... PHP is run server side and is fully executed before any data is sent to the browser.... Javascript only starts to run once the data is sent to the browser.....

    It is possible to set javascript variables in php, but not the other way round.

    I suggest that you re-write the function in php.... or... here is another solution.....add this at the end of the function

    Code:
    window.location('/somefile.php?num='+num); // I am not sure if this is the correct syntax


    then you can retrieve the variable num in php using $_GET['num']


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •