Using Javascript value in Php but how ?

Discussion in 'PHP' started by babaMBA, Aug 23, 2007.

  1. #1
    <script language="javascript">
    function changeRanking(a,b)
    {
    var newA = 100;
    var newB = 200;
    }
    </script>

    I have this javascript function, It is working fine.

    This function execute on "onChange" event of a text field.

    I want to use "newA" and "newB" value in my php code after that <script> block.

    How can i do that. Plz help me
     
    babaMBA, Aug 23, 2007 IP
  2. Kendothpro

    Kendothpro Well-Known Member

    Messages:
    574
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    120
    #2
    At runtime or after the page has loaded? You could try using AJAX
     
    Kendothpro, Aug 23, 2007 IP
  3. Coder

    Coder Banned

    Messages:
    311
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That is not possible. Client side values you can't use in server side scripts...
    You can just print them out using getElementByID Method..
     
    Coder, Aug 23, 2007 IP
  4. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #4
    They would have to be posted or sent via the URL - AJAX would probably be the best solution for this

    Brew
     
    Brewster, Aug 23, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    You can also pass values via GET using dummy images.

    
    document.getElementById('dummy').src = 'script.php?foo='+ bar;
    
    Code (javascript):
    But if you actually need a response value from the script, your only option is AJAX.
     
    nico_swd, Aug 23, 2007 IP
  6. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    If you use a form and hidden fields inside the form to store these values on client side, then the contend will be available by posting on server side.

    Example:

    Javascript should say:
    document.getElementById('newA').value = 150;

    HTML should say:
    <MENU action = "POST" ...>
    <INPUT TYPE='HIDDEN' ID="newA" ...>
    <INPUT TYPE='SUBMIT' ... >
    </MENU>

    PHP should be able to retrieve your values simply by using the variable $newA
    alert("value on server side: ".$newA);
     
    webrickco, Aug 23, 2007 IP