SitePoint Sponsor

User Tag List

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

    access javascript variable in php


    <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!'

    <!-- GET THE JSON DATA -->
    <script src=''></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?


  2. #2
    SitePoint Evangelist mrwooster's Avatar
    Join Date
    Jan 2006
    0 Post(s)
    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

    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']


Posting Permissions

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