SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Trash Boat mkoenig's Avatar
    Join Date
    Aug 2007
    Posts
    1,232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Passing JavaScript Variables to PHP

    Passing JavaScript Variables to PHP

    This is a very tricky thing, as javascript is browser/user side, and php is serverside.

    I know you can do it because i've done similar with ajax in the past. (myabe i just did request i don't remember)

    Anyway, does anyone know how to pass javascript embeded in a page to a remote servers php code?

    Thanks SitePoint

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It depends upon the context.

    At which point is JavaScript supposed to send variables to PHP?

    Is the PHP script meant to output something? If so, is the page sending the variables meant to output this somewhere?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Assuming you're talking about a non-AJAX implementation, and retrieving the variables from Javascript after a page reload.... Use the javascript to write out a value into a hidden form field, and use PHP to retrieve it from the $_POST.

  4. #4
    Trash Boat mkoenig's Avatar
    Join Date
    Aug 2007
    Posts
    1,232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jake Arkinstall View Post
    It depends upon the context.

    At which point is JavaScript supposed to send variables to PHP?

    Is the PHP script meant to output something? If so, is the page sending the variables meant to output this somewhere?
    PHP is meant to receive the request and input it into a database.

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Is there a specific reason you don't want to do this through a normal form?

    Also, are you using any JavaScript libraries such as JQuery? They make AJAX requests easier, so they're useful.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    Trash Boat mkoenig's Avatar
    Join Date
    Aug 2007
    Posts
    1,232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Im trying to create a google analytic's type app

    so i want it super light to reduce pageloads so no jquery or other library containing non essential content

    and cant use a standard form because the script will be included on remote webpages and is supposed to require no action on the users behalf

    (this post just got moved from the PHP part of the forum)

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    (new Image).src = "http://......?foo=" + encodeURIComponent(foo);

  8. #8
    Trash Boat mkoenig's Avatar
    Join Date
    Aug 2007
    Posts
    1,232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    (new Image).src = "http://......?foo=" + encodeURIComponent(foo);
    So make it think its an image and pass the variables to it?

    if so thanks man, ill give it a try


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
  •