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 3 of 3
  1. #1
    Regular Coder Jazzo's Avatar
    Join Date
    Apr 2008
    Location
    New York City
    Posts
    164
    Thanks
    20
    Thanked 2 Times in 2 Posts

    Assign Javascript Variable to PHP

    I have a javascript variable that I need to pass to a php variable. Since PHP is server-side, I figured the only way to send the js variable is to refresh. However, I don't want to reload the page. How can I send a javascript variable to php on the same page without reloading? For example, I want to assign the js variable "com" to the PHP variable "$com".

    Thanks,
    Jazzo

    P.S. So far, I've defined my Ajax request as such:
    Code:
    req = (function () {
      if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
      }
      else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
      }
      else {
        return null;
      }
    }());
    Last edited by Jazzo; 03-13-2010 at 01:33 AM.
    ~Julian
    14 y/o web developer, drummer, and Bridge player

  • #2
    New Coder
    Join Date
    May 2008
    Posts
    80
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Your code is not enough to request anything from the server let alone doing something useful when you get a response. How are you passing/handling the variables. I guess you're going to use a form. You can pass values to the server either with get/post and php will receive it in $_GET/$_Post variable. Try to get a xmlhttprequest structure which you can make your own. Everytime I see how others make them and I go or . If you get that, much is already done. And afterwards you need some understanding of DOM.

  • #3
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    On one site has found a good script can help you: Passing Javascript variable to php


  •  

    Posting Permissions

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