0

I am wondering how to transition a variable from PHP to Jquery.

My php file is loaded into a div like this:

$("#leaguesSelectionTable th").click(function(){
        var teamSelect = this.id;
$("#loadTables").load("php/leagueTable.php?team="+teamSelect);
});

When the leagueTable.php is loaded into the div, I echoed and made sure the variable gets there. It does.

<?php if(isset($_GET['team'])){
    $team = $_GET['team'];
}?>

leagueTable.php also has its own .js file with more functions. I want to know if I can get the $team variable in leagueTable.php to the .js file (which runs on document ready) and stored into a variable. Something like:

$.get( "../leagueTable.php", function( data ) {
  var = data;
});

What I really don't get about ajax calls is how do I specify the particular data I want from the PHP file? I only want to retrive the $team variable and store it in a jquery variable.

Thanks.

2
  • 1
    You need to take your php data and output to a format easily parseable by the jQuery library. I'd recommend turning your php data into json php.net/manual/en/ref.json.php since jQuery can easily interact with this data type api.jquery.com/jquery.getjson Commented Mar 26, 2014 at 3:20
  • Okay so I've gone ahead and done json_encode($team); Now in my next step, something like: $.getJSON("../leagueTable.php", function (data){ var teamSelect = .....? Commented Mar 26, 2014 at 3:26

3 Answers 3

1

simply

    <?php if(isset($_GET['team'])){
        echo $_GET['team'];exit;
     }?>

and try this to check if you are getting correct values

     $.get( "../leagueTable.php", function( data ) {
          console.log(data);
     });
Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain "data" to me? Is data just everything and anything from PHP? What if I only want one var? Furthermore I don't want to echo a variable, it will display on the page right? heh.
nope, it will not display on page.. if you are sending ajax request, "data" is the param where we are collecting the response of request. if you will hit this page on browser, then it will echo the $_GET['team'].
0

The load method will expect HTML from the provided URL, to be embedded in the element provided, #loadTables in this case.

The script element is completely valid and the browser will probably execute what ever is inside immediately after appending the new DOM elements.

So, the context where this new JS is executed should be the same as the one where you made the AJAX call. If I am not wrong, you could globally declare a variable before calling the load method and the sub-script included in the AJAX response should be able to access it.

Though, I think this is probably not the very best approach you could use.

Comments

0
<script type="text/javascript">
$("#leaguesSelectionTable th").click(function(){
        var teamSelect = this.id;
 $.get( "php/leagueTable.php?team="+teamSelect", function( data ) {
var teamSelect = jQuery.parseJSON(data);
 });

});
<script>

OR you can simply use like below:

var teamSelect = jQuery.parseJSON('<?php echo $teamSelect;?>');

Here you will have to prepare the $teamSelect in the php file as a JSON string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.