SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast cfpmedia.com's Avatar
    Join Date
    Nov 2010
    Location
    California, USA
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Assign Variable to PHP

    Hi there,

    I was wondering if it were possible to assign a PHP variable's value to that of a JS variable.
    What I mean is having a var in JS, and then making a PHP variable equal to the JS one.

    Like this:
    Code:
    function username() {
    var username = "Mr. Jones";
    
    <?php $username = username; ?>
    
    }
    Now of course, this file will be .php, and will be called as such when including it to a page (<script type="text/javascript" src="username.php").

    The above code doesn't work... But I want to know if it can be done in such a manner.
    I just want a PHP variable to be equal to a variable in JS.

    Any ideas?

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,080
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Without something like AJAX there is no way you can do this.
    What is it that you ultimately would like to achieve with this? Maybe there are other ways
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Enthusiast cfpmedia.com's Avatar
    Join Date
    Nov 2010
    Location
    California, USA
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I was hoping to quickly determine if a username exists with the use of JavaScript and PHP... Without having to reload the page.

  4. #4
    SitePoint Addict MrBaseball34's Avatar
    Join Date
    Sep 2002
    Location
    Round Rock, Texas
    Posts
    277
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Remember, you are OUTPUTTING javascript from PHP, so you just need to echo the value.

    If you are using the technique I showed in my reply to your other post, it is a no brainer.
    Show more of your current code.
    MrBaseball34
    Hook'Em Horns!

  5. #5
    SitePoint Addict MrBaseball34's Avatar
    Join Date
    Sep 2002
    Location
    Round Rock, Texas
    Posts
    277
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree with ScallioXTX that this functionality is best suited for AJAX.
    MrBaseball34
    Hook'Em Horns!

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,080
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by cfpmedia.com View Post
    Well, I was hoping to quickly determine if a username exists with the use of JavaScript and PHP... Without having to reload the page.
    Yes in that case you indeed need AJAX.
    Here's a tutorial on how to that with jQuery: check username availability in ajax and php | ajax username checker
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Enthusiast cfpmedia.com's Avatar
    Join Date
    Nov 2010
    Location
    California, USA
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, thanks.


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
  •