Transfer Javascript value to PHP

Discussion in 'PHP' started by nimonogi, Nov 28, 2007.

  1. #1
    Hello,

    How i can transfer a Javascript value to PHP?

    I'm using the following code but for some reason is not working:
    
    <script>
    function ServerLocation()
    {
    var ServLoc="none";
    if (document.ServLink.link[0].checked)
    ServLoc=ServLink.link[0].value;
    if (document.ServLink.link[1].checked)
    ServLoc=ServLink.link[1].value;
    
    location=ServLoc;
    }
    
    var ServerLocation = "<?=$PHPServerLocation?>";
    </script>
    
    Code (markup):
    and after a radio button is checked i echo the $PHPServerLocation
    
    <?php echo $PHPServerLocation; ?>
    
    Code (markup):
    Thanks in advance!
     
    nimonogi, Nov 28, 2007 IP
  2. theOtherOne

    theOtherOne Well-Known Member

    Messages:
    112
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi,

    this won't work, because every line of PHP is executed before the page is sent to your browser. So when you are able to click on an element such as a checkbox, PHP is done and is not influenced by your actions.

    To pass a variable to PHP you would need to reload the page with a parameter defining the value, or use ajax-like methods.
     
    theOtherOne, Nov 28, 2007 IP
  3. nimonogi

    nimonogi Active Member

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    80
    #3
    So the only solution is to create a link (value is a link URL) that gets the javascript value?

    
    <a href="#" onClick="ServerLocation()">Link</a>
    
    Code (markup):
     
    nimonogi, Nov 28, 2007 IP
  4. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #4
    Use Ajax.

    Peace,
     
    Barti1987, Nov 28, 2007 IP
  5. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #5
    try this one...

    
    function ServerLocation()
    {
    var ServLoc="none";
    if (document.ServLink.link[0].checked)
    ServLoc=ServLink.link[0].value;
    if (document.ServLink.link[1].checked)
    ServLoc=ServLink.link[1].value;
    
    location=ServLoc;
    }
    var ServerLocation = "urphppage.php?valuename="+valuetobepassed;
    xmlHttp.open("GET",ServerLocation,"true");
    
    PHP:
    on the urphppage.php

    
    echo $_GET['valuename'];
    
    PHP:
    hope this helps..
     
    bartolay13, Nov 28, 2007 IP
  6. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #6
    oh and by the way, thats ajax in simple form..
     
    bartolay13, Nov 28, 2007 IP