how to pass a javascriot variable to php function

Discussion in 'jQuery' started by jean_ibar, Dec 14, 2012.

  1. #1
    Hi
    Here an sample of my code test.php:
    
    [RIGHT][COLOR=blue][U]Copy cod[/U][/COLOR]
    [/RIGHT]
    
    [LIST=1]
    [*]$test = "coucou"; 
    [*]print '<script type="text/javascript" language="javascript">
                    $(document).ready(function()
                    { 
    [*]                    $("#tablelines").find("tr").each(function()
                        { 
    [*]                      var test = " ' . $test . ' " ; 
    [*]                      alert(test); 
    [*]                      ................. 
    [*]                      var rowid = $(this).attr("id"); 
    [*]                      var rowidstr = " ' . $this->UnitGetDescr([B]rowid[/B]) . ' " ; 
    [*]                      .......................... 
    [*]                     })
                        ....................................
                    })
             </script>'; 
    [/LIST]
    
    Code (markup):
    on line 8 i want pass a javascript variable rowid to my php function UnitGetDescr() !
    but i do not find the good syntax.
    Can you help me ?
    for example on line 5 my program display 'coucou' string.
    thanks
     
    jean_ibar, Dec 14, 2012 IP
  2. zied.ho

    zied.ho Well-Known Member

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    106
    #2
    $.get('yourfunction.php?var='+[COLOR=#111111][FONT=monospace]rowidstr[/FONT][/COLOR], function(data) {
      $('.result').html(data);  //data is the response 
      alert('Load was performed.');
    });
    
    Code (markup):
    your php function call must be like this : UnitGetDescr($_GET['var'])
     
    zied.ho, Dec 16, 2012 IP
  3. jean_ibar

    jean_ibar Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hi,

    thanks, but i do not work

    i do not understand what is : yourfunction.php?var=

    i must replace by : $this->UnitGetDescr.php?var=

    the $.get has an url paramater and not a php function ?

    thanks for your explain.
     
    jean_ibar, Dec 17, 2012 IP
  4. zied.ho

    zied.ho Well-Known Member

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    106
    #4
    can you first , explain me how do you pass a variable to your function without javascript ? thx
     
    zied.ho, Dec 17, 2012 IP
  5. jean_ibar

    jean_ibar Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    hi,
    sorry but i do not understand your question.
    so my last question about your sample code is : by which i replace 'yourfunction.php' ?
    thanks
     
    jean_ibar, Dec 18, 2012 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    PHP runs on the server, generating code to send to the browser. Javascript is part of that code, and it runs in the browser. When Javascript is running, PHP has finished and is no longer running.

    If UnitGetDescr() is a Javascript function, you just call it. If it's a PHP function, you have to use AJAX to call the PHP file that the function is in. (What you do after that depends on what the function does, and what it's used for on the site.) You CAN NOT call a PHP function in Javascript directly - they don't talk to each other as if they were running at the same time.

    Search for AJAX and learn how to use it. You're using jQuery so, after you learn how AJAX works, look at jQuery;s ajax() function to see how trivial it is to implement it in jQuery.
     
    Rukbat, Dec 20, 2012 IP
  7. jean_ibar

    jean_ibar Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    hi,ok thanks, i've understandthanks
     
    jean_ibar, Dec 20, 2012 IP