SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    passing javascript variables to php script

    I have set things up so that on my html page i call a php page via a js script call. If i want to include variables to pass to the php script, currently i include them in the url string (process.php?id=12...) and then use $_GET['id'] to get this in my php script.

    <script type="text/javascript" src="process.php?id=12&color=000"></script>

    However, i would like to seperate out the variables like so:

    <script type="text/javascript"><!--
    id = "12";
    color = "000";
    --></script>

    <script type="text/javascript" src="process.php"></script>

    How can i get these variables into my php script?

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If you do it like that, process.php can't access those variables. Something like this would work:

    Code:
    <script type="text/javascript">
    id = '12';
    color = '000';
    
    var el = document.createElement('script');
    el.type = 'text/javascript';
    el.src = 'process.php?id=' + id + '&color=' + color;
    document.body.appendChild(el);
    </script>
    Although if process.php displays something, you might not want to just append the element to the body. The other alternative is AJAX.

  3. #3
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I ended up using 2 js files and then a php file!


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
  •