#1
  1. No Profile Picture
    UberGeek
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Posts
    67
    Rep Power
    14

    Question Setting PHP variables with Javascript?


    Is there a way to set a PHP variable with javascript? I'd like to do something like this:

    <SCRIPT LANGUAGE='Javascript'>
    <!--
    function set_fruit( fruit )
    {
    $this_fruit = fruit;
    return true;
    }
    //-->
    </SCRIPT>

    <A HREF="www.foo.com" onClick='set_fruit( "Apple" );'>
    Apple</A>


    where $this_fruit is a PHP session variable.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Location
    Redmond, WA
    Posts
    218
    Rep Power
    14
    No. You can't do that. You can use PHP to set a Javascript variable but it can't be done the other way around.
  4. #3
  5. No Profile Picture
    Gödelian monster
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 1999
    Location
    Central Florida, USA
    Posts
    2,307
    Rep Power
    61
    Hi Sano, I'm going to be a pain in the neck and contradict you again, sorry .

    Yes, you can do it, if you follow the (somewhat contorted) methods discussed in this thread: Using Javascript with PHP

    Sano is technically right. PHP and Javascript can't directly interact, but if you use the HTTP mechanisms available in the right way, you can accomplish your goal in a way that is fairly seamless to the end-user. See our lively discussion about this topic at can you run regular php code inside a script tag?

    HTH
    The real n-tier system:

    FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

    Amazon wishlist -- rycamor (at) gmail.com
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Location
    Redmond, WA
    Posts
    218
    Rep Power
    14
    I knew you'd pop up somewhere!

    At least you acknowledged I was technically right
  8. #5
  9. No Profile Picture
    Gödelian monster
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 1999
    Location
    Central Florida, USA
    Posts
    2,307
    Rep Power
    61
    What do you mean "at least"? (lol)
    The real n-tier system:

    FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

    Amazon wishlist -- rycamor (at) gmail.com
  10. #6
  11. No Profile Picture
    UberGeek
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Posts
    67
    Rep Power
    14

    Talking


    Thanks guys!

IMN logo majestic logo threadwatch logo seochat tools logo