Welcome to the IMTalk - Internet Marketing & SEO Forum.
  • Login:
+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Trek is offline IM & SEO Quiet One Trek is on a distinguished road
    Join Date
    Jul 2011
    Posts
    20
    Thanks Given
    1
    Thanked 0 Times in 0 Posts

    How to pass a PHP variable to Javascript

    Was wondering how i'd go about passing a php variable to my javascript. For example, i have the following code:

    Code:
    $("#sort").change(function() {
          window.location =  "/page.php" + $("#sort").val();
        });
    How could I go about using a PHP variable to include the "/page.php" instead of having to define it manually for every page I would like to use it on?

    I've tried a few things but seem to be failing pretty bad lol, for example things like:

    Code:
    $("#sort").change(function() {
          window.location =  "<?php echo $page ?>" + $("#sort").val();
        });
    and obviously are not working

    Any help is greatly appreciated, Thanks!

  2. #2
    Nemanja's Avatar
    Nemanja is offline IMTalk.org Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute
    Join Date
    Jun 2010
    Location
    www
    Posts
    4,870
    Thanks Given
    703
    Thanked 3,044 Times in 1,247 Posts
    This is the way to go:
    Code:
    $("#sort").change(function() {
          window.location = "<?= $page ?>" + $("#sort").val();
        });

  3. #3
    Trek is offline IM & SEO Quiet One Trek is on a distinguished road
    Join Date
    Jul 2011
    Posts
    20
    Thanks Given
    1
    Thanked 0 Times in 0 Posts
    Thanks for the response, Nemanja!

    Tried using the code, and seemed to try to sort (page refreshed and such as it should) but didn't seem to pass the variable as defined by the php to the javascript.

    When the page refreshed it tried actually using the exact string that is in the quotes (page url looked like: domain DOT com/<?= $page ?>/s-sort/so-sortorder)

    My file structure is as follows, maybe that has to do with something. Pretty much, trying to define the page URL in my .php page, pass it to my main include page and have my main JS file reference the php variable that i have defined and sort. The code given seems to be trying to process, given the page refreshing and such but does not seem to be grabbing the variable.

    Do I need to include some sort of incline script to have my js utilize the variable in the file in which the code is in?

    Thanks for the help!

  4. #4
    Nemanja's Avatar
    Nemanja is offline IMTalk.org Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute
    Join Date
    Jun 2010
    Location
    www
    Posts
    4,870
    Thanks Given
    703
    Thanked 3,044 Times in 1,247 Posts
    Quote Originally Posted by Trek View Post
    Thanks for the response, Nemanja!

    Tried using the code, and seemed to try to sort (page refreshed and such as it should) but didn't seem to pass the variable as defined by the php to the javascript.

    When the page refreshed it tried actually using the exact string that is in the quotes (page url looked like: domain DOT com/<?= $page ?>/s-sort/so-sortorder)

    My file structure is as follows, maybe that has to do with something. Pretty much, trying to define the page URL in my .php page, pass it to my main include page and have my main JS file reference the php variable that i have defined and sort. The code given seems to be trying to process, given the page refreshing and such but does not seem to be grabbing the variable.

    Do I need to include some sort of incline script to have my js utilize the variable in the file in which the code is in?

    Thanks for the help!
    Then pass php var outside the function.

    var page = "<?= $page ?>";

    $("#sort").change(function() {
    window.location = page + $("#sort").val();
    });

  5. The Following User Says Thank You to Nemanja For This Useful Post:


  6. #5
    Trek is offline IM & SEO Quiet One Trek is on a distinguished road
    Join Date
    Jul 2011
    Posts
    20
    Thanks Given
    1
    Thanked 0 Times in 0 Posts
    Awesome, works like a charm

    Thanks for the help, Nemanja!

  7. #6
    Nemanja's Avatar
    Nemanja is offline IMTalk.org Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute Nemanja has a reputation beyond repute
    Join Date
    Jun 2010
    Location
    www
    Posts
    4,870
    Thanks Given
    703
    Thanked 3,044 Times in 1,247 Posts
    Quote Originally Posted by Trek View Post
    Awesome, works like a charm

    Thanks for the help, Nemanja!
    No problems


 

Similar Threads

  1. Facebook will easily pass Yahoo
    By scottman in forum Social Networks & Community Websites
    Replies: 12
    Last Post: 08-02-2012, 06:37 AM
  2. Replies: 10
    Last Post: 08-14-2011, 09:51 PM

Posting Permissions

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