Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post

    passing a JS var to PHP

    I have a JS var and I want to pass it to PHP for a mySQL select

    Code:
    function foo (boo){
    
         <?php
        $sql = "SELECT boogie FROM man where id = boo";
        $result = mysql_query($sql);
         ?>
    
         ...
    
    }

    can boo be passed to PHP? and if so, how?

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    you could use window.location to add querystring to url and use $_GET['varname'];
    simple

  • #3
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post
    Quote Originally Posted by rafiki View Post
    you could use window.location to add querystring to url and use $_GET['varname'];
    simple

    there are no URLs here... this code is all in one script ...

    I have a JS var which I pass into a function

    I want to use that same var in PHP inside this function

  • #4
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    If you're trying to use javascript to pass to some PHP on the same page "you can't" without reloading the same page. PHP (server-side) is finished parsing by the time javascript(client-side) comes into play.

    However if you want to use PHP variable in javascript, you can just echo it out to the JS.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #5
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    Quote Originally Posted by StupidRalph View Post
    If you're trying to use javascript to pass to some PHP on the same page "you can't" without reloading the same page. PHP (server-side) is finished parsing by the time javascript(client-side) comes into play.

    However if you want to use PHP variable in javascript, you can just echo it out to the JS.
    hence the window.location post above


  •  

    Posting Permissions

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