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?

1
  • ...aaaaand AJAX is file3 ? Commented Jan 21, 2016 at 22:54

4 Answers 4

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;
?>
Sign up to request clarification or add additional context in comments.

6 Comments

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)
Thank you, changed that :).
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?
Which function is called twice exactly :)?
Hi Kevin, It is not calling twice. When I use the dropdown for the first time (<select name="speler1" onchange="getInfo(this.value); getRank(this.value)"> the program doesn't show anything. When I use again, he shows the rank of the user before
|
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);
       }
     });         
};

Comments

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>

Comments

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

1 Comment

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.

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.