1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

java variable to php variable

Discussion in 'PHP' started by JEET, Feb 26, 2006.

  1. #1
    Hello,
    I have a value in a javascript variable :

    var myname = "jeet";
    ( The value is a result of a Javascript function.)
    I want this value in php variable $my .
    Anyone knows how to do this ?
    Thank you
    jeet
     
    JEET, Feb 26, 2006 IP
  2. Ilija Studen

    Ilija Studen Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Please provide more info about what you want to do. Just an example. There are really few options (sending data through GET or POST, XMLHttpRequest, JS inclusion...)
     
    Ilija Studen, Feb 26, 2006 IP
  3. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3
    Hi,
    There's a function for example sake lets say "functionname()" in javascript .
    A parameter is being sent to this function lets say "text".
    Finally a variable named "myname is getting the value of the output like:

    var myname= functionname(text);
    Now I want to send this value to a PHP variable named "$myn"
    Can you suggest something here?

    regards
    jeet
     
    JEET, Feb 26, 2006 IP
  4. rossriley

    rossriley Guest

    Messages:
    25
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'm assuming that you mean you want to pass a javascript value to the next php process.
    Obviously the only way to do this is via a request. You have three options 1 -POST, 2 - GET or 3 -XMLHttpRequest.
    1. You need to set up a hidden textfield in a form called myn which when the javascript value changes also copies the new value to the hidden field, obviously for this method the user would have to submit a form, when they do the value of mny could be intercepted by the next php process and $mny could be set to the value of $_POST['mny'].

    2. As above really except the value can be appended onto the end of a link... some dom scripting would be needed for this but then the value would be sent when the user clicks on the link.

    3. The final way may not be applicable, you may have come across an XMLHttpRequest as being called an AJAX request. This is the most tricky, you would need to implement an ajax library to send off your request to a php script.

    So it all depends upon which of these methods is most suitable for your situation......

    Ross
     
    rossriley, Feb 26, 2006 IP
  5. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #5
    Hi,
    Thanks for help.
    I took the first suggestion putting a hidden form field and it works now.

    Regards
    jeet
     
    JEET, Feb 26, 2006 IP
  6. rajeev_seo

    rajeev_seo Peon

    Messages:
    211
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    David, the only way that you are going to be able to get a value out of JavaScript (which runs on the client) back into PHP (which runs on the server) is by a page load. You asked how to get the JavaScript Result back into PHP. When Result is assigned a value (let's say 55), you need to make a JavaScript call to redirect the browser:

    Code:

    <script language="JavaScript">
    var Result = 55;
    location.href="myPage.php?Result=" + Result;
    </script>

    You can then use the $_GET superglobal array to get the value of Result.
     
    rajeev_seo, Apr 24, 2011 IP