2

I want to pass a variable from file1.php to file2.php using jquery.

file1.php

<?php
    $user_rank = $rank;
?>

file2.php

<?php
    $user_rank = $_GET['user_rank'];
?>

AJAX

function getRank()
{
$.ajax({
       type: "GET",
       url: "file2.php",
       data: ?????,
       success: function(result){
         $("#TargetRank").html(result);
       }
     });         
};

Can anyone help me with this?

Chris G
  • 787
  • 6
  • 20
Sara Z
  • 625
  • 7
  • 18

4 Answers4

2

The passing part can happen in the script where the variable is defined, so in file1.php. Then you get the following files:

file1.php:

<?php
$user_rank = 123;
?>
<script>
function getRank()
{
$.ajax({
       type: "GET",
       url: "file2.php?user_rank=<?php echo $user_rank; ?>",
       success: function(result){
         $("#TargetRank").html(result);
       }
     });         
};
</script>

file2.php:

<?php
$user_rank = $_GET['user_rank'];
echo $user_rank;
?>
www.data-blogger.com
  • 4,076
  • 7
  • 43
  • 65
  • 1
    You can easily contact one PHP file (or even by simply using `include`) and after receiving the response > send that response to PHP file 2 (using in total 3 files, logically) – Roko C. Buljan Jan 21 '16 at 22:51
  • Thank you, changed that :). – www.data-blogger.com Jan 21 '16 at 22:55
  • Thank you Kevin. It works almost, but it has nothing to do with your code. I call with onChange two function in the same time. The first function create the variable $user_rank and the second one getRank(). The problem is that the value of user_rank shows on the page after calling the function TWICE. Do you know what the problem is? – Sara Z Jan 22 '16 at 00:13
  • Which function is called twice exactly :)? – www.data-blogger.com Jan 22 '16 at 05:44
  • Hi Kevin, It is not calling twice. When I use the dropdown for the first time ( – Sara Z Jan 22 '16 at 06:19
  • I believe for this case I should not use the both functions at the same time. First getInfo() to create the variable, then the function getRank(), but how to do that with onchange? – Sara Z Jan 22 '16 at 06:24
0

I'm guessing the Javascript code is used in file1.php. Then your question becomes more like "How do I pass a PHP variable to Javascript?". The best way I have seen is with a "data element" in the DOM.

Add this to file1.php (somewhere logicalish)

<span id="user-rank" data-rank="<?= $user_rank ?>"></span>

Then you can grab that value in your JS

function getRank()
{
var rank = $("#user-rank").attr("data-rank");
$.ajax({
       type: "GET",
       url: "file2.php?user_rank="+rank,
       success: function(result){
         $("#TargetRank").html(result);
       }
     });         
};
Dan
  • 10,614
  • 5
  • 24
  • 35
0

Assuming your AJAX is in file1.php you could do this:

file1.php

<?php
    $user_rank = $rank;
?>

<script>
    function getRank()
    {
        $.ajax({
            type: "GET",
            url: "file2.php",
            data: {user_rank: '<?php echo $user_rank; ?>'},
            success: function(result){
                $("#TargetRank").html(result);
            }
        });         
    }
</script>
j_quelly
  • 1,399
  • 4
  • 16
  • 37
0
  1. On file1.php output the variables as JSON (see Returning JSON from a PHP Script )

  2. Then on the JavaScript do an ajax call to read the variables to an object (let's call it data).

  3. Then do your call placing the data variable where you have the ????? .

Example:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
    <script>

    $(function(){
        $.getJSON( "http://dev/json.php", function( data ) {
            $.ajax({
                type:'GET',
                url:'json2.php',
                data: data,
                success: function(data){
                    console.log(data);
                }
            });
        });
    });
    </script>
</head>
<body>
    <div id="content"></div>
</body>
</html>

json.php

<?php
header('Content-Type: application/json');
echo '{"name":"Telmo Dias"}';

json2.php

<?php print_r($_GET); ?>

Result: Result

Community
  • 1
  • 1
Telmo Dias
  • 3,938
  • 2
  • 36
  • 48
  • You could simply send a GET to file1, wait for a raw response, on success transfer the data to file2.php (and even than wait for a success response). Even simpler? php `include` file1 and place the variable into the JS. Send to file2 and wait for success. – Roko C. Buljan Jan 21 '16 at 22:59