#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    3
    Rep Power
    0

    passing variables from Javascript to PHP


    I have this JS code

    <SCRIPT language="JavaScript1.2">
    <!--
    if (navigator.appName == 'Netscape')
    var language = navigator.language;
    else
    var language = navigator.systemLanguage;

    document.write("" + language);
    //-->
    </SCRIPT>

    this should prints something like en-us for example


    i wanna get that "en-us" in a php variale

    how can I do this?

    thanx in advance
  2. #2
  3. 300lb Bench!
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2001
    Location
    New York
    Posts
    2,350
    Rep Power
    62
    A lot of times I like to know what a person is doing. That way, I can say something like, "Why are you doing it that way? Why don't you just try...". In any event, to answer your question, try

    PHP Code:
          1 <script language="JavaScript">
          2 function setvariable()
          3 {
          4   document.write(42);
          5 }
          6 </script>
          7 <html>
          8 <body>
          9 <form name="form1">
         10 </form>
         11 </body>
         12 </html>
         13 <?php
         14 $x 
    "<script>setvariable()</script>";
         
    15 print "The value is: " $x;
         
    16 ?>
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    3
    Rep Power
    0

    Smile


    thanx for your help
  6. #4
  7. No Profile Picture
    Overly white
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Fresno, CA
    Posts
    83
    Rep Power
    12
    I've never used PHP before but I was wondering about your response. Is X really equal to 42? Or is it just writing the script tags to the browser and then javascript is displaying 42? That would be really strange because Javascript is client side code. Could you provide an example of when you would use this? Thanks.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    3
    Rep Power
    0
    this is what I used and it worked but not as I expected

    <SCRIPT>
    <!--
    function setvariable()
    {
    if (navigator.appName == 'Netscape')
    var language = navigator.language;
    else
    var language = navigator.systemLanguage;

    document.write("" + language);
    }
    //-->
    </SCRIPT>

    <?php
    $language = "<script>setvariable()</script>";
    ?>

    the problem I got now that I cannot deal with language as a string. for example I tried split or explode:
    list($language_code,$country) = split("-", $language);

    I always get $language_code = "<script>setvariable()</script>"
    Last edited by sorial; April 8th, 2003 at 01:55 AM.

IMN logo majestic logo threadwatch logo seochat tools logo